This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw, ImageFont | |
import matplotlib.pyplot as plt | |
import io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# generate video | |
create_video_from_images("/images", "out.mp4") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_video_from_images(images_folder, output_video, fps=10, duration=6): | |
images = sorted(os.listdir(images_folder)) | |
# create path to the input images | |
img_path = os.path.join(images_folder, images[0]) | |
# load image | |
frame = cv2.imread(img_path) | |
# extract dimensions of the image | |
height, width, channels = frame.shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 # (OpenCV) version - 4.7.0 | |
import os |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cnt = 0 | |
for img in tqdm_notebook(col_images): | |
# apply frame mask | |
masked = cv2.bitwise_and(img[:,:,0], img[:,:,0], mask=stencil) | |
# apply image thresholding | |
ret, thresh = cv2.threshold(masked, 130, 145, cv2.THRESH_BINARY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# specify frame index | |
idx = 457 | |
# plot frame | |
plt.figure(figsize=(10,10)) | |
plt.imshow(col_images[idx][:,:,0], cmap= "gray") | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_relation(sent): | |
doc = nlp(sent) | |
# Matcher class object | |
matcher = Matcher(nlp.vocab) | |
#define the pattern | |
pattern = [{'DEP':'ROOT'}, | |
{'DEP':'prep','OP':"?"}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remove punctuation marks | |
punctuation = '!"#$%&()*+-/:;<=>?@[\\]^_`{|}~' | |
train['clean_tweet'] = train['clean_tweet'].apply(lambda x: ''.join(ch for ch in x if ch not in set(punctuation))) | |
test['clean_tweet'] = test['clean_tweet'].apply(lambda x: ''.join(ch for ch in x if ch not in set(punctuation))) | |
# convert text to lowercase | |
train['clean_tweet'] = train['clean_tweet'].str.lower() | |
test['clean_tweet'] = test['clean_tweet'].str.lower() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# extract subject | |
source = [i[0] for i in entity_pairs] | |
# extract object | |
target = [i[1] for i in entity_pairs] | |
kg_df = pd.DataFrame({'source':source, 'target':target, 'edge':relations}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# predict next token | |
def predict(net, tkn, h=None): | |
# tensor inputs | |
x = np.array([[token2int[tkn]]]) | |
inputs = torch.from_numpy(x) | |
# push to GPU | |
inputs = inputs.cuda() |