Spaces:
Sleeping
Sleeping
Create camera.py
Browse files
camera.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
|
3 |
+
class VideoCamera:
|
4 |
+
def __init__(self, camera_type='user'):
|
5 |
+
self.video = cv2.VideoCapture(0) # Default to webcam for testing
|
6 |
+
self.camera_type = camera_type
|
7 |
+
|
8 |
+
def __del__(self):
|
9 |
+
self.video.release()
|
10 |
+
|
11 |
+
def get_frame(self):
|
12 |
+
success, image = self.video.read()
|
13 |
+
if success:
|
14 |
+
ret, jpeg = cv2.imencode('.jpg', image)
|
15 |
+
return jpeg.tobytes()
|
16 |
+
return None
|
17 |
+
|
18 |
+
def switch_camera(self, camera_type):
|
19 |
+
self.video.release()
|
20 |
+
self.camera_type = camera_type
|
21 |
+
self.video = cv2.VideoCapture(0) # Update for browser-based switching in JS
|