Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import base64
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
def capture_video():
|
| 7 |
+
# Open the default camera
|
| 8 |
+
cap = cv2.VideoCapture(0)
|
| 9 |
+
|
| 10 |
+
# Loop through frames
|
| 11 |
+
while True:
|
| 12 |
+
# Read the current frame
|
| 13 |
+
ret, frame = cap.read()
|
| 14 |
+
|
| 15 |
+
# Encode the frame as a jpeg
|
| 16 |
+
_, buffer = cv2.imencode('.jpg', frame)
|
| 17 |
+
|
| 18 |
+
# Convert the buffer to a Base64 string
|
| 19 |
+
encoded_image = base64.b64encode(buffer).decode('utf-8')
|
| 20 |
+
# print("~~~~~~~OK~~~~~~~~~~")
|
| 21 |
+
# print(type(encoded_image))
|
| 22 |
+
|
| 23 |
+
# Do something with the encoded image, for example, send it to an API endpoint
|
| 24 |
+
# print(encoded_image)
|
| 25 |
+
|
| 26 |
+
response = requests.post("https://abidlabs-pytorch-image-classifier.hf.space/api/predict", json={
|
| 27 |
+
"data": [(encoded_image)
|
| 28 |
+
|
| 29 |
+
]
|
| 30 |
+
}).json()
|
| 31 |
+
print(response)
|
| 32 |
+
|
| 33 |
+
# Display the frame
|
| 34 |
+
cv2.imshow('frame', frame)
|
| 35 |
+
|
| 36 |
+
# Exit the loop if 'q' is pressed
|
| 37 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 38 |
+
break
|
| 39 |
+
|
| 40 |
+
# Release the capture and destroy the window
|
| 41 |
+
cap.release()
|
| 42 |
+
cv2.destroyAllWindows()
|
| 43 |
+
|
| 44 |
+
iface = gr.Interface(fn=capture_video, inputs="webcam", outputs="text")
|
| 45 |
+
iface.launch()
|