-
Notifications
You must be signed in to change notification settings - Fork 0
/
gptmod.py
60 lines (50 loc) · 1.99 KB
/
gptmod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
import cv2
from keras.models import load_model
# Load pre-trained YOLO model and set classes
net = cv2.dnn.readNet("/home/mandarin/Documents/opencv/yolov3.cfg", "/home/mandarin/Documents/opencv/yolov3.weights")
classes = []
with open("/home/mandarin/Documents/opencv/coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# Load pre-trained neural network for NLG
model = load_model("/path/to/pretrained/model.h5")
# Define function to extract objects from image
def extract_objects(image):
# Preprocess image using YOLO
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
outs = net.forward(net.getUnconnectedOutLayersNames())
# Extract objects using YOLO
objects = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
object_name = classes[class_id]
objects.append((object_name, detection))
return objects
# Define function to generate sentence based on detected objects
def generate_sentence(objects):
# Create input vector for NLG model
input_vec = np.zeros((1, len(objects), 5))
for i, obj in enumerate(objects):
class_id = classes.index(obj[0])
x, y, w, h = obj[1][:4]
input_vec[0][i] = [class_id, x, y, w, h]
# Generate sentence using NLG model
sentence = ""
while "endseq" not in sentence:
output = model.predict([input_vec, np.zeros((1, 1))])
output_token = np.argmax(output[0, -1, :])
word = classes[output_token]
sentence += word + " "
input_vec[0][len(sentence.split()) - 1][0] = output_token
return sentence.strip()
# Load image and extract objects
image = cv2.imread("/path/to/image.jpg")
objects = extract_objects(image)
# Generate sentence based on objects
sentence = generate_sentence(objects)
print(sentence)