Spaces:
Running
Running
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() |