-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
78 lines (55 loc) · 2.16 KB
/
main.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
from transformers import ViltProcessor, ViltForQuestionAnswering
import cv2
import threading
# disable warnings
import warnings
warnings.filterwarnings("ignore")
user_input = ""
ans = ""
frame = None
# Create a function to take user input
def get_user_input():
global user_input
while True:
user_input = input("Enter your command: ")
print("You entered: ", user_input)
if user_input == 'q':
break
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
# Open a connection to the webcam
cap = cv2.VideoCapture(0)
# Create a thread to take user input
input_thread = threading.Thread(target=get_user_input)
input_thread.start()
def capture_frame():
while True:
# Read a frame from the webcam
global frame
_, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Display the frame in the window
cv2.putText(frame, "Question: ", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, user_input, (110, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 2)
cv2.putText(frame, "Answer: ", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, ans, (110, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow("Webcam Feed", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
# Check if the user pressed the 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
def process_question():
global ans
while True:
if user_input.strip() != "":
encoding = processor(frame, user_input, return_tensors="pt")
outputs = model(**encoding)
logits = outputs.logits
idx = logits.argmax(-1).item()
ans = model.config.id2label[idx]
# print("Predicted answer:", ans)
frame_thread = threading.Thread(target=capture_frame)
question_thread = threading.Thread(target=process_question)
frame_thread.start()
question_thread.start()