File size: 1,235 Bytes
bee2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()