Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import requests
|
4 |
+
|
5 |
+
# Start the webcam feed
|
6 |
+
cap = cv2.VideoCapture(0)
|
7 |
+
|
8 |
+
while True:
|
9 |
+
ret, frame = cap.read()
|
10 |
+
if not ret:
|
11 |
+
print("Failed to grab frame.")
|
12 |
+
break
|
13 |
+
|
14 |
+
# Process the frame if needed
|
15 |
+
# For example, convert to grayscale:
|
16 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
17 |
+
|
18 |
+
# Send the frame to Hugging Face API for inference
|
19 |
+
# Convert the frame to a format suitable for the API (e.g., a base64-encoded image)
|
20 |
+
_, buffer = cv2.imencode('.jpg', frame)
|
21 |
+
frame_bytes = buffer.tobytes()
|
22 |
+
|
23 |
+
# Define your Hugging Face API endpoint and model
|
24 |
+
model_endpoint = "https://api-inference.huggingface.co/models/your-model"
|
25 |
+
|
26 |
+
headers = {
|
27 |
+
"Authorization": "Bearer your-huggingface-api-token"
|
28 |
+
}
|
29 |
+
|
30 |
+
# Send frame as a POST request to Hugging Face
|
31 |
+
response = requests.post(model_endpoint, headers=headers, files={"file": ("frame.jpg", frame_bytes, "image/jpeg")})
|
32 |
+
|
33 |
+
if response.status_code == 200:
|
34 |
+
result = response.json() # Process the result
|
35 |
+
print(result) # Do something with the result, like display it
|
36 |
+
else:
|
37 |
+
print("Error:", response.status_code, response.text)
|
38 |
+
|
39 |
+
# Show the webcam feed (with potential processed data)
|
40 |
+
cv2.imshow("Webcam Feed", gray_frame)
|
41 |
+
|
42 |
+
# Exit if 'q' is pressed
|
43 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
44 |
+
break
|
45 |
+
|
46 |
+
# Release the webcam and close any OpenCV windows
|
47 |
+
cap.release()
|
48 |
+
cv2.destroyAllWindows()
|