Skip to content

Commit c1ab4a3

Browse files
committed
* add uvc app
1 parent 04bc7f2 commit c1ab4a3

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

projects/usb_video_camera/app.png

3.9 KB
Loading

projects/usb_video_camera/app.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
id: usb_video_camera
2+
name: USB Video Camera
3+
version: 1.0.1
4+
author: 916BGAI
5+
icon: app.png
6+
desc: Display the image of the USB camera
7+
files:
8+
- main.py

projects/usb_video_camera/main.py

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
from maix import image, display, app, time, touchscreen
2+
import cv2, os
3+
from datetime import datetime
4+
5+
disp = display.Display()
6+
ts = touchscreen.TouchScreen()
7+
8+
back_btn_pos = (0, 0, 130, 40) # x, y, w, h
9+
back_btn_disp_pos = image.resize_map_pos(disp.width(), disp.height(), disp.width(), disp.height(), image.Fit.FIT_CONTAIN, back_btn_pos[0], back_btn_pos[1], back_btn_pos[2], back_btn_pos[3])
10+
option_btn_pos = (disp.width()-120, 0, 120, 40)
11+
option_btn_disp_pos = image.resize_map_pos(disp.width(), disp.height(), disp.width(), disp.height(), image.Fit.FIT_CONTAIN, option_btn_pos[0], option_btn_pos[1], option_btn_pos[2], option_btn_pos[3])
12+
save_btn_pos = (disp.width()-90, disp.height()-40, 90, 40)
13+
save_btn_disp_pos = image.resize_map_pos(disp.width(), disp.height(), disp.width(), disp.height(), image.Fit.FIT_CONTAIN, save_btn_pos[0], save_btn_pos[1], save_btn_pos[2], save_btn_pos[3])
14+
frame1_btn_disp_pos = image.resize_map_pos(disp.width(), disp.height(), disp.width(), disp.height(), image.Fit.FIT_CONTAIN, int(disp.width()/4), int(disp.height()/4), int(disp.width()/2), int(disp.height()/5))
15+
frame2_btn_disp_pos = image.resize_map_pos(disp.width(), disp.height(), disp.width(), disp.height(), image.Fit.FIT_CONTAIN, int(disp.width()/4), int(disp.height()/4)+int(disp.height()/5), int(disp.width()/2), int(disp.height()/5))
16+
frame3_btn_disp_pos = image.resize_map_pos(disp.width(), disp.height(), disp.width(), disp.height(), image.Fit.FIT_CONTAIN, int(disp.width()/4), int(disp.height()/4)+2*int(disp.height()/5), int(disp.width()/2), int(disp.height()/5))
17+
18+
pressed_flag = [False, False, False, False, False, False]
19+
option_flag = False
20+
save_flag = False
21+
save_time = 0
22+
23+
def draw_btns(img : image.Image):
24+
img.draw_rect(back_btn_pos[0], back_btn_pos[1], back_btn_pos[2], back_btn_pos[3], image.Color.from_rgb(255, 255, 255), 2)
25+
img.draw_string(back_btn_pos[0] + 4, back_btn_pos[1] + 8, "< back", image.COLOR_WHITE, 2)
26+
img.draw_rect(option_btn_pos[0], option_btn_pos[1], option_btn_pos[2], option_btn_pos[3], image.Color.from_rgb(255, 255, 255), 2)
27+
img.draw_string(option_btn_pos[0] + 4, option_btn_pos[1] + 8, "Option", image.COLOR_WHITE, 2)
28+
img.draw_rect(save_btn_pos[0], save_btn_pos[1], save_btn_pos[2], save_btn_pos[3], image.Color.from_rgb(255, 255, 255), 2)
29+
img.draw_string(save_btn_pos[0] + 4, save_btn_pos[1] + 8, "Save", image.COLOR_WHITE, 2)
30+
31+
def is_in_button(x, y, btn_pos):
32+
return x > btn_pos[0] and x < btn_pos[0] + btn_pos[2] and y > btn_pos[1] and y < btn_pos[1] + btn_pos[3]
33+
34+
def on_touch(x, y, pressed):
35+
global pressed_flag, learn_id
36+
if pressed:
37+
if is_in_button(x, y, back_btn_disp_pos):
38+
pressed_flag[0] = True
39+
elif is_in_button(x, y, option_btn_disp_pos):
40+
pressed_flag[1] = True
41+
elif is_in_button(x, y, save_btn_disp_pos):
42+
pressed_flag[2] = True
43+
elif is_in_button(x, y, frame1_btn_disp_pos):
44+
pressed_flag[3] = True
45+
elif is_in_button(x, y, frame2_btn_disp_pos):
46+
pressed_flag[4] = True
47+
elif is_in_button(x, y, frame3_btn_disp_pos):
48+
pressed_flag[5] = True
49+
else: # cancel
50+
pressed_flag = [False, False, False, False, False, False]
51+
else:
52+
if pressed_flag[0]:
53+
print("back btn click")
54+
pressed_flag[0] = False
55+
return True, False, False, False, False, False
56+
elif pressed_flag[1]:
57+
print("option btn click")
58+
pressed_flag[1] = False
59+
return False, True, False, False, False, False
60+
elif pressed_flag[2]:
61+
print("save btn click")
62+
pressed_flag[2] = False
63+
return False, False, True, False, False, False
64+
elif pressed_flag[3]:
65+
print("frame1 btn click")
66+
pressed_flag[3] = False
67+
return False, False, False, True, False, False
68+
elif pressed_flag[4]:
69+
print("frame2 btn click")
70+
pressed_flag[4] = False
71+
return False, False, False, False, True, False
72+
elif pressed_flag[5]:
73+
print("frame3 btn click")
74+
pressed_flag[5] = False
75+
return False, False, False, False, False, True
76+
return False, False, False, False, False, False
77+
78+
img = image.Image(disp.width(), disp.height(), image.Format.FMT_RGB888)
79+
80+
if os.path.exists("/boot/usb.host"):
81+
print("USB is in Host mode.")
82+
else:
83+
print("Please set USB to Host mode.")
84+
img.draw_rect(0, 0, disp.width(), disp.height(), image.Color.from_bgr(17, 17, 17), -1)
85+
img.draw_string(20, int(disp.height()*0.45), "Please set USB to Host mode.", image.COLOR_WHITE, 2)
86+
draw_btns(img)
87+
disp.show(img)
88+
while True:
89+
x, y, pressed = ts.read()
90+
back, option, _, _, _, _ = on_touch(x, y, pressed)
91+
if back:
92+
exit()
93+
time.sleep_ms(10)
94+
95+
cap = cv2.VideoCapture(0)
96+
97+
if not cap.isOpened():
98+
print("can't open the camera")
99+
img.draw_rect(0, 0, disp.width(), disp.height(), image.Color.from_bgr(17, 17, 17), -1)
100+
img.draw_string(20, int(disp.height()*0.45), "Please check if the camera is connected.", image.COLOR_WHITE, 2)
101+
draw_btns(img)
102+
disp.show(img)
103+
while True:
104+
x, y, pressed = ts.read()
105+
back, option, _, _, _, _ = on_touch(x, y, pressed)
106+
if back:
107+
exit()
108+
time.sleep_ms(10)
109+
110+
porp_frame = [(320, 240), (640, 480), (1920, 1080)]
111+
frame_option = (320, 240)
112+
113+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
114+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
115+
116+
while not app.need_exit():
117+
ret, frame = cap.read()
118+
if not ret:
119+
print("can't read the frame")
120+
continue
121+
122+
img = image.cv2image(frame, True, False)
123+
img_resize = img.resize(disp.width(), disp.height())
124+
125+
x, y, pressed = ts.read()
126+
back, option, save, frame1, frame2, frame3 = on_touch(x, y, pressed)
127+
if back:
128+
break
129+
elif option:
130+
option_flag = not option_flag
131+
elif save:
132+
picture_root_path = app.get_picture_path()
133+
picture_date = datetime.now().strftime("%Y-%m-%d")
134+
picture_path = os.path.join(picture_root_path, picture_date)
135+
if not os.path.exists(picture_path):
136+
os.makedirs(picture_path)
137+
file_list = os.listdir(picture_path)
138+
picture_save_path = os.path.join(picture_path, f"{len(file_list)}.jpg")
139+
print(f"picture_save_path path: {picture_save_path}")
140+
img.save(picture_save_path)
141+
save_flag = True
142+
save_time = time.ticks_ms()
143+
144+
if option_flag == True:
145+
img_resize.draw_rect(int(disp.width()/4), int(disp.height()/8), int(disp.width()/2), int(disp.height()/1.2), image.Color.from_rgb(17, 17, 17), -1)
146+
img_resize.draw_string(int(disp.width()/3.1), int(disp.height()/6), "Set Resolution", image.COLOR_WHITE, 1.6)
147+
img_resize.draw_string(int(disp.width()/2.9), int(disp.height()/3.2), "320 x 240", image.COLOR_WHITE, 2)
148+
img_resize.draw_string(int(disp.width()/2.9), int(disp.height()/3.2)+int(disp.height()/5), "640 x 480", image.COLOR_WHITE, 2)
149+
img_resize.draw_string(int(disp.width()/3.2), int(disp.height()/3.2)+2*int(disp.height()/5), "1920 x 1080", image.COLOR_WHITE, 2)
150+
151+
if frame_option == porp_frame[0]:
152+
img_resize.draw_string(int(disp.width()/2.9), int(disp.height()/3.2), "320 x 240", image.COLOR_GREEN, 2)
153+
elif frame_option == porp_frame[1]:
154+
img_resize.draw_string(int(disp.width()/2.9), int(disp.height()/3.2)+int(disp.height()/5), "640 x 480", image.COLOR_GREEN, 2)
155+
elif frame_option == porp_frame[2]:
156+
img_resize.draw_string(int(disp.width()/3.2), int(disp.height()/3.2)+2*int(disp.height()/5), "1920 x 1080", image.COLOR_GREEN, 2)
157+
158+
if frame1:
159+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, porp_frame[0][0])
160+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, porp_frame[0][1])
161+
frame_option = porp_frame[0]
162+
print(f"set frame to {porp_frame[0][0]} x {porp_frame[0][1]}")
163+
option_flag = False
164+
elif frame2:
165+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, porp_frame[1][0])
166+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, porp_frame[1][1])
167+
frame_option = porp_frame[1]
168+
print(f"set frame to {porp_frame[1][0]} x {porp_frame[1][1]}")
169+
option_flag = False
170+
elif frame3:
171+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, porp_frame[2][0])
172+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, porp_frame[2][1])
173+
frame_option = porp_frame[2]
174+
print(f"set frame to {porp_frame[2][0]} x {porp_frame[2][1]}")
175+
option_flag = False
176+
177+
if save_flag == True:
178+
img_resize.draw_rect(int(disp.width()/4), int(disp.height()/6), int(disp.width()/2), int(disp.height()/6), image.COLOR_WHITE, -1)
179+
img_resize.draw_string(int(disp.width()/3.4), int(disp.height()/4.7), "Save Success", image.COLOR_BLACK, 2)
180+
if time.ticks_ms()-save_time >= 1000:
181+
save_flag = False
182+
183+
draw_btns(img_resize)
184+
disp.show(img_resize)
185+
186+
cap.release()

0 commit comments

Comments
 (0)