File size: 1,708 Bytes
a8e02b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
import gradio as gr
import requests
from io import BytesIO

def abnormal_stream(image):
    try:
        byte_io = BytesIO()
        image.save(byte_io, 'png')
        byte_io.seek(0)

        r = requests.post(
            'https://6a051cv20250210-prediction.cognitiveservices.azure.com/customvision/v3.0/Prediction/29f565b7-4710-47a5-8a47-723048ff7ec9/classify/iterations/Iteration2/image',
            headers={
                'Prediction-Key': '8uyKSiqRNbG2JLdMjI8AeOzADtORP3jRh5klqQr0JsJrBBt7x7iPJQQJ99BBACYeBjFXJ3w3AAAIACOGHg4K',
                'Content-Type': 'application/octet-stream',
            },
            data=byte_io,
        )

        if r.status_code != 200:
            return {'확인불가': 1.0, r.status_code: 0.0, r.text: 0.0}, None

        output_dict = {}

        for item in r.json()['predictions']:
            tag_name = item['tagName']
            probability = item['probability']
            output_dict[tag_name] = probability

        with open('alert.wav', 'rb') as f:
            output_audio = f.read()
            return output_dict, output_audio

    except Exception as e:
        return {str(e): 1.0}

with gr.Blocks(
    analytics_enabled=False,
    title='졸음운전 알리미',
    head='''<meta name="theme-color" content="#0f0f11">''',
) as demo:
    with gr.Row():
        with gr.Column():
            input_img = gr.Image(sources=["webcam"], type="pil")
        with gr.Column():
            output_label = gr.Label()
            output_audio = gr.Audio(autoplay=True)
        dep = input_img.stream(abnormal_stream, [input_img], [output_label, output_audio])

if __name__ == "__main__":
    demo.launch(favicon_path='./favicon.png', show_api=False)