File size: 2,591 Bytes
d14d8cc
38d9ba0
d14d8cc
a0a091e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd276da
 
a0a091e
bd276da
 
 
 
 
 
 
 
 
 
00fdca9
bd276da
 
709b3ae
bd276da
00fdca9
bd276da
 
 
 
d14d8cc
bd276da
 
 
 
 
a0a091e
 
 
 
 
 
 
 
ca40b1b
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
import requests

# def abnormal(image):
#     if (image is None) or (image == ''):
#         return {'이미지가 제공되지 않았습니다.': 1.0}

#     try:
#         with open(image, 'rb') as f:
#             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=f.read(),
#             )

#         if r.status_code != 200:
#             return {'확인불가': 1.0}

#         output_dict = {}

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

#         return output_dict

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

# demo = gr.Interface(abnormal, gr.Image(label="Input Image Component", type="filepath", sources=["webcam"]), "label")

def abnormal_stream(image):
    if (image is None) or (image == ''):
        return {'이미지가 제공되지 않았습니다.': 1.0}

    try:
        with open(image, 'rb') as f:
            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=f.read(),
            )

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

        output_dict = {}

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

        return output_dict

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

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            input_img = gr.Image(sources=["webcam"], type="filepath")
        with gr.Column():
            output_img = gr.Label()
        dep = input_img.stream(abnormal_stream, [input_img], [output_img], every=1)

demo.launch()