IOT / app.py
Kunal09's picture
Create app.py
bee2a97
raw
history blame
No virus
1.24 kB
import gradio as gr
import cv2
import base64
import requests
def capture_video():
# Open the default camera
cap = cv2.VideoCapture(0)
# Loop through frames
while True:
# Read the current frame
ret, frame = cap.read()
# Encode the frame as a jpeg
_, buffer = cv2.imencode('.jpg', frame)
# Convert the buffer to a Base64 string
encoded_image = base64.b64encode(buffer).decode('utf-8')
# print("~~~~~~~OK~~~~~~~~~~")
# print(type(encoded_image))
# Do something with the encoded image, for example, send it to an API endpoint
# print(encoded_image)
response = requests.post("https://abidlabs-pytorch-image-classifier.hf.space/api/predict", json={
"data": [(encoded_image)
]
}).json()
print(response)
# Display the frame
cv2.imshow('frame', frame)
# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and destroy the window
cap.release()
cv2.destroyAllWindows()
iface = gr.Interface(fn=capture_video, inputs="webcam", outputs="text")
iface.launch()