File size: 1,198 Bytes
569320d
 
 
 
 
 
7624331
 
 
 
 
 
 
569320d
 
7624331
569320d
 
 
 
 
 
 
 
7624331
569320d
 
 
 
7624331
 
569320d
 
 
 
 
 
 
 
 
 
 
 
af25ad9
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
import gradio as gr
import requests
from io import BytesIO
import base64
import matplotlib.image as mpimg
import cv2
import numpy as np

def img_to_string(img):
    # Encodes an image into a base64_string
    _, encoded_img = cv2.imencode('.PNG', img)
    base64_string = base64.b64encode(encoded_img).decode('utf-8')
    return base64_string

def string_to_img(base64_string):
    # Decodes a base64_string into an image
    imgdata = base64.b64decode(base64_string + '==')
    im = BytesIO(imgdata)
    img = mpimg.imread(im, format='PNG')
    opencv_img= cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
    return opencv_img

def api_image_processing(image):
    # API Endpoint
    url = "http://13.42.31.235/fmc_api"
    # Step 1
    b64_string = img_to_string(image)
    #  Step 2
    payload ={"base64_string": b64_string}
    response = requests.post(url=url, data=payload) 
    base64_string_resp = response.json()['message']
    # Step 3
    img_output = string_to_img(base64_string_resp)
    # Return processed image
    return img_output

demo = gr.Interface(
    api_image_processing, 
    gr.Image(source="webcam", streaming=True), 
    "image",
    live=True
)

demo.launch(debug=True)