-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api_webcam.py
77 lines (55 loc) · 2.08 KB
/
test_api_webcam.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
# Chat test program
# Date: 2024/10/09
# Author: [email protected]
import sys
sys.path.append("../../rag")
from urllib import parse
import requests
import base64
import cv2
import unittest
BASE_URL = "http://localhost:5050"
SAVE_IMAGE = False
IMG_CAP_WIDTH = 1280
IMG_CAP_HEIGHT = 720
IMG_TARGET_HEIGHT = 512
IMG_TARGET_WIDTH = int(IMG_TARGET_HEIGHT / IMG_CAP_HEIGHT * IMG_CAP_WIDTH)
class TestChat(unittest.TestCase):
"""Test the APIs
"""
def test_chat_with_webcam_image_without_context(self):
"""Test chat with webcam connected to this computer.
Webcam: Buffalo BSW200MBK
https://www.buffalo.jp/product/detail/bsw200mbk.html
"""
# Video Capture from USB Webcam
cap = cv2.VideoCapture(0)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)
print(f"[Initial resolution] width: {width}, height: {height}, fps: {fps}")
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(f"[Higher resolution] width: {width}, height: {height}, fps: {fps}")
result, img = cap.read()
cap.release()
if result is False:
raise Exception("Video capture error!")
else:
img_resized = cv2.resize(img, (IMG_TARGET_WIDTH, IMG_TARGET_HEIGHT))
retval, jpg_img= cv2.imencode('.jpg', img_resized)
b64image = base64.b64encode(jpg_img).decode('utf-8')
if SAVE_IMAGE:
with open("./tmp/cap.jpg", "wb") as f:
f.write(jpg_img)
urlparams = parse.urlencode({
"user_message": "What can you see in the attached image?",
})
r = requests.put(f"{BASE_URL}/chat?{urlparams}",
json={"b64image": b64image})
print(r.json())
self.assertEqual(r.status_code, 200)
if __name__ == "__main__":
unittest.main()