Spaces:
Paused
Paused
| import cv2 | |
| import numpy as np | |
| import requests | |
| # Start the webcam feed | |
| cap = cv2.VideoCapture(0) | |
| while True: | |
| ret, frame = cap.read() | |
| if not ret: | |
| print("Failed to grab frame.") | |
| break | |
| # Process the frame if needed | |
| # For example, convert to grayscale: | |
| gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| # Send the frame to Hugging Face API for inference | |
| # Convert the frame to a format suitable for the API (e.g., a base64-encoded image) | |
| _, buffer = cv2.imencode('.jpg', frame) | |
| frame_bytes = buffer.tobytes() | |
| # Define your Hugging Face API endpoint and model | |
| model_endpoint = "https://api-inference.huggingface.co/models/your-model" | |
| headers = { | |
| "Authorization": "Bearer your-huggingface-api-token" | |
| } | |
| # Send frame as a POST request to Hugging Face | |
| response = requests.post(model_endpoint, headers=headers, files={"file": ("frame.jpg", frame_bytes, "image/jpeg")}) | |
| if response.status_code == 200: | |
| result = response.json() # Process the result | |
| print(result) # Do something with the result, like display it | |
| else: | |
| print("Error:", response.status_code, response.text) | |
| # Show the webcam feed (with potential processed data) | |
| cv2.imshow("Webcam Feed", gray_frame) | |
| # Exit if 'q' is pressed | |
| if cv2.waitKey(1) & 0xFF == ord('q'): | |
| break | |
| # Release the webcam and close any OpenCV windows | |
| cap.release() | |
| cv2.destroyAllWindows() | |