import gradio as gr import requests from PIL import Image import io import cv2 import numpy as np fire_count = 0 def plot_one_box(x, img, color=None, label=None, score=None, line_thickness=3): # Plots one bounding box on image img tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness color = color c1, c2 = (int(x[0]), int(x[1])), (int(x[2])+int(x[0]), int(x[3])+int(x[1])) cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) if label: tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [0, 0, 0], thickness=tf, lineType=cv2.LINE_AA) pro = f"{score:.3f}" t_size = cv2.getTextSize(pro, 0, fontScale=tl / 3, thickness=tf)[0] c1 = c2 c2 = c1[0] + t_size[0], c1[1] + t_size[1] + 3 cv2.rectangle(img, c1, c2, [0, 255, 255], -1, cv2.LINE_AA) # filled cv2.putText(img, pro, (c1[0], c2[1] - 2), 0, tl / 3, [0, 0, 0], thickness=tf, lineType=cv2.LINE_AA) return img def fire(frame): global fire_count fire_count = fire_count + 1 print("fire_count", fire_count) url = "http://127.0.0.1:8080/fire" file = {'file': open(frame, 'rb')} r = requests.post(url=url, files=file) fire_output = None result = r.json().get('result') object_name = r.json().get('class') box = r.json().get('coordinate') pro = r.json().get('score') # print("\n number: ", plate_number) # print("\n coordinate: ", box) # print("\n score: ", pro) try: image = cv2.imread(frame, cv2.IMREAD_COLOR) if image is None: print('image is null') sys.exit() # image = cv2.resize(image, (1024, 640)) for obj_name in object_name: # print(plate_number) if object_name[obj_name]: if object_name[obj_name] == "fire": image = plot_one_box(box[obj_name], image, label=object_name[obj_name], score=pro[obj_name], color=[0, 255, 0], line_thickness=1) else: image = plot_one_box(box[obj_name], image, label=object_name[obj_name], score=pro[obj_name], color=[0, 0, 255], line_thickness=1) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) fire_output = image.copy() except: pass return fire_output with gr.Blocks() as demo: gr.Markdown( """ # KBY-AI Fire/Smoke Detection SDK Demo We offer SDKs for face recognition, liveness detection(anti-spoofing), ID card recognition and ID document liveness detection. We also specialize in providing outsourcing services with a variety of technical stacks like AI(Computer Vision/Machine Learning), mobile apps, and web apps. ##### KYC Verification Demo - https://github.com/kby-ai/KYC-Verification-Demo-Android ##### ID Capture Web Demo - https://cap.kby-ai.com """ ) with gr.TabItem("Fire/Smoke Detection"): gr.Markdown( """ ##### Docker Hub - https://hub.docker.com/r/kbyai/fire-smoke-detection ```bash sudo docker pull kbyai/fire-smoke-detection:latest sudo docker run -v ./license.txt:/home/openvino/kby-ai-fire/license.txt -p 8081:8080 -p 9001:9000 kbyai/fire-smoke-detection:latest ``` """ ) with gr.Row(): with gr.Column(): fire_image_input = gr.Image(type='filepath', height=300) gr.Examples(['fire_examples/test1.jpg', 'fire_examples/test2.jpg', 'fire_examples/test3.jpg', 'fire_examples/test4.jpg'], inputs=fire_image_input) fire_confirmation_button = gr.Button("Confirm") with gr.Column(): fire_output = gr.Image(type="numpy") fire_confirmation_button.click(fire, inputs=fire_image_input, outputs=fire_output) gr.HTML('') demo.launch(server_name="0.0.0.0", server_port=7860)