-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathannotate_webcam_stream.py
88 lines (73 loc) · 2.49 KB
/
annotate_webcam_stream.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import pyttsx3
import engineio
import requests
import cv2
from io import BytesIO
import matplotlib.pyplot as plt
from PIL import Image
from invoke import run
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", type=int, default=7777, help="Port of caption generator API")
parser.add_argument("-e", "--engine", type=int, default=0, help="0 if you want to use the 'say' Speech Synthesis Manager engine, 1 if you want to use pyttsx3")
args = parser.parse_args()
url_coco = 'http://localhost:{}/generate_caption_ms_coco'.format(args.port)
url_cc = 'http://localhost:{}/generate_caption_conceptual_captions'.format(args.port)
special_toks = ('xxunk', 'xxpad', 'xxbos', 'xxfld', 'xxmaj', 'xxup', 'xxrep', 'xxwrep', '-')
if args.engine == 1:
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
#engine.setProperty('rate', 150)
elif args.engine > 1:
print("Invalid TTS engine chosen")
sys.exit()
def remove_special_toks(cap):
return ' '.join([w for w in cap.split() if w not in special_toks])
def say_caption(caption):
if args.engine == 1:
engine.say(caption)
engine.runAndWait()
else:
cmd = "say -v Alex {}".format(caption)
result = run(cmd, hide=True, warn=True)
def get_caption(img, url):
imgByteArr = BytesIO()
img.save(imgByteArr, format='JPEG')
imgByteArr.seek(0)
response = requests.post(url, files={'input_image': imgByteArr})
if response.ok:
return response.text
else:
return "Could not connect to API"
cam = cv2.VideoCapture(0)
cam.open(0)
cv2.namedWindow("Window_1", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Window_1", 960,600)
while True:
ret, frame = cam.read()
cv2.imshow("Window_1", frame)
if not ret:
break
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k%256 == 32:
# SPACE pressed
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = Image.fromarray(frame, 'RGB')
caption = get_caption(frame, url_coco)
say_caption(remove_special_toks(caption))
print(caption)
elif k%256 == 13:
# RET pressed
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = Image.fromarray(frame, 'RGB')
caption = get_caption(frame, url_cc)
say_caption(remove_special_toks(caption))
print(caption)
cam.release()
cv2.destroyAllWindows()