-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
150 lines (117 loc) · 4.27 KB
/
test2.py
File metadata and controls
150 lines (117 loc) · 4.27 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import time
import speech_recognition as sr
import torch
import threading
import cv2
from PIL import Image
import sounddevice as sd
from scipy.io.wavfile import write
import numpy as np
import regex as re
import keyboard as keys
from transformers.utils import logging
logging.set_verbosity(0)
from transformers import pipeline, AutoModelForZeroShotObjectDetection, AutoProcessor
'''
use device_index=0 (Microsoft Sound Mapper Input Microphone)
use device_index=1 (AMD Audio Dev Input)
'''
# some globals
mutex_lock = threading.Lock()
boxes = []
def voice_chat(recognizer, microphone):
with microphone as source:
print("Speak Fool....")
audio = recognizer.listen(source)
time.sleep(5)
print("Recognizing...")
time.sleep(1)
text = recognizer.recognize_google(audio)
return text
def record_audio(duration=5, sample_rate=16000, save_as_wav=False):
print("Recording in 2 seconds... Speak after the beep.")
time.sleep(1)
print("Beep!")
audio = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1, dtype='float32')
sd.wait()
if audio is None or len(audio) == 0:
raise RuntimeError("No audio was recorded.")
if save_as_wav:
write("my_audio.wav", sample_rate, audio)
return np.squeeze(audio)
def save_frames_to_video(frames, output_path="temp_video.mp4", fps=24):
height, width, _ = frames[0].shape
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
for frame in frames:
out.write(frame)
out.release()
return output_path
def start_video_feed():
vid_cap = cv2.VideoCapture(0)
while vid_cap.isOpened():
ret, frame = vid_cap.read()
frame = cv2.flip(frame, 1)
if not ret:
break
with mutex_lock:
cv2.imwrite('my_image.jpg', frame)
# Draw boxes from object detection
for box in boxes:
frame = cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 3)
cv2.imshow("cam_feed", frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
vid_cap.release()
# recognizer = sr.Recognizer()
# mic = sr.Microphone(device_index=1)
# print("press s to enable voice chat")
# flag = input()
# if flag=='s':
# print("Opening Voice Chat")
# time.sleep(1)
# audio = record_audio(save_as_wav=True)
# print(audio)
# print(text)
# matches = re.findall('(?i)(?:x|y)\s*(?:equals|equals to|=)\s*(\d+)', text)
# print(matches)
# print(audio)
def object_detection():
model_name = "IDEA-Research/grounding-dino-base"
device = "cuda" if torch.cuda.is_available() else "cpu"
processor = AutoProcessor.from_pretrained(model_name)
model = AutoModelForZeroShotObjectDetection.from_pretrained(model_name).to(device)
while True:
time.sleep(2)
with mutex_lock:
image_path = 'my_image.jpg'
try:
image = Image.open(image_path)
except:
continue # if image is not ready or corrupted
text = 'a bottle.' # VERY important: text queries need to be lowercased + end with a dot
inputs = processor(images=image, text=text, return_tensors="pt").to(device)
with torch.no_grad():
outputs = model(**inputs)
results = processor.post_process_grounded_object_detection(
outputs,
inputs.input_ids,
box_threshold=0.4,
text_threshold=0.3,
target_sizes=[image.size[::-1]]
)[0]
print(results)
new_boxes = []
for box, label, score in zip(results["boxes"], results["labels"], results["scores"]):
box = box.to("cpu").numpy().astype(int) # pixels topLeft and bottomRight)
new_boxes.append(box)
with mutex_lock:
boxes.clear()
boxes.extend(new_boxes)
cam_thread = threading.Thread(target=start_video_feed)
detect_thread = threading.Thread(target=object_detection)
cam_thread.start()
detect_thread.start()
cam_thread.join()
detect_thread.join()