Spaces:
Sleeping
Sleeping
| import cv2 | |
| import numpy as np | |
| class VideoCamera(object): | |
| def __init__(self): | |
| self.video = cv2.VideoCapture(0) | |
| if not self.video.isOpened(): | |
| print("Warning: Could not open camera. Using placeholder.") | |
| def __del__(self): | |
| if self.video.isOpened(): | |
| self.video.release() | |
| def get_frame(self): | |
| if self.video.isOpened(): | |
| success, image = self.video.read() | |
| if success: | |
| ret, jpeg = cv2.imencode('.jpg', image) | |
| return jpeg.tobytes() | |
| # Create placeholder if camera not available | |
| image = np.zeros((480, 640, 3), dtype=np.uint8) | |
| cv2.putText(image, "CAMERA NOT AVAILABLE", (100, 240), | |
| cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) | |
| ret, jpeg = cv2.imencode('.jpg', image) | |
| return jpeg.tobytes() |