Spaces:
Runtime error
Runtime error
File size: 1,467 Bytes
057949d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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()
|