Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from PIL import Image
|
4 |
+
from ultralyticsplus import YOLO
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
7 |
+
from transformers import pipeline
|
8 |
+
import requests
|
9 |
+
from io import BytesIO
|
10 |
+
import os
|
11 |
+
|
12 |
+
model = YOLO('corn20epoch.pt')
|
13 |
+
name = ['Corn Rust','Grey Leaf Spot','Leaf Blight']
|
14 |
+
# image_directory = "/home/user/app/image"
|
15 |
+
# video_directory = "/home/user/app/video"
|
16 |
+
|
17 |
+
# url_example="https://drive.google.com/file/d/1bBq0bNmJ5X83tDWCzdzHSYCdg-aUL4xO/view?usp=drive_link"
|
18 |
+
# url_example='https://drive.google.com/uc?id=' + url_example.split('/')[-2]
|
19 |
+
# r = requests.get(url_example)
|
20 |
+
# im1 = Image.open(BytesIO(r.content))
|
21 |
+
|
22 |
+
# url_example="https://drive.google.com/file/d/16Z7QzvZ99fbEPj1sls_jOCJBsC0h_dYZ/view?usp=drive_link"
|
23 |
+
# url_example='https://drive.google.com/uc?id=' + url_example.split('/')[-2]
|
24 |
+
# r = requests.get(url_example)
|
25 |
+
# im2 = Image.open(BytesIO(r.content))
|
26 |
+
|
27 |
+
# url_example="https://drive.google.com/file/d/13mjTMS3eR0AKYSbV-Fpb3fTBno_T42JN/view?usp=drive_link"
|
28 |
+
# url_example='https://drive.google.com/uc?id=' + url_example.split('/')[-2]
|
29 |
+
# r = requests.get(url_example)
|
30 |
+
# im3 = Image.open(BytesIO(r.content))
|
31 |
+
|
32 |
+
# url_example="https://drive.google.com/file/d/1-XpFsa_nz506Ul6grKElVJDu_Jl3KZIF/view?usp=drive_link"
|
33 |
+
# url_example='https://drive.google.com/uc?id=' + url_example.split('/')[-2]
|
34 |
+
# r = requests.get(url_example)
|
35 |
+
# im4 = Image.open(BytesIO(r.content))
|
36 |
+
# for i, r in enumerate(results):
|
37 |
+
|
38 |
+
# # Plot results image
|
39 |
+
# im_bgr = r.plot()
|
40 |
+
# im_rgb = im_bgr[..., ::-1] # Convert BGR to RGB
|
41 |
+
|
42 |
+
|
43 |
+
def response2(image: gr.Image = None,image_size: gr.Slider = 640, conf_threshold: gr.Slider = 0.3, iou_threshold: gr.Slider = 0.6):
|
44 |
+
|
45 |
+
results = model.predict(image, conf=conf_threshold, iou=iou_threshold, imgsz=image_size)
|
46 |
+
|
47 |
+
text = ""
|
48 |
+
name_weap = ""
|
49 |
+
|
50 |
+
box = results[0].boxes
|
51 |
+
|
52 |
+
for r in results:
|
53 |
+
im_array = r.plot()
|
54 |
+
im = Image.fromarray(im_array[..., ::-1])
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
for r in results:
|
59 |
+
conf = np.array(r.boxes.conf.cpu())
|
60 |
+
cls = np.array(r.boxes.cls.cpu())
|
61 |
+
cls = cls.astype(int)
|
62 |
+
xywh = np.array(r.boxes.xywh.cpu())
|
63 |
+
xywh = xywh.astype(int)
|
64 |
+
|
65 |
+
for con, cl, xy in zip(conf, cls, xywh):
|
66 |
+
cone = con.astype(float)
|
67 |
+
conef = round(cone,3)
|
68 |
+
conef = conef * 100
|
69 |
+
text += (f"Detected {name[cl]} with confidence {round(conef,1)}% at ({xy[0]},{xy[1]})\n")
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
# xywh = int(results.boxes.xywh)
|
75 |
+
# x = xywh[0]
|
76 |
+
# y = xywh[1]
|
77 |
+
|
78 |
+
return im, text
|
79 |
+
|
80 |
+
|
81 |
+
inputs = [
|
82 |
+
gr.Image(type="pil", label="Input Image"),
|
83 |
+
gr.Slider(minimum=320, maximum=1280, value=640,
|
84 |
+
step=32, label="Image Size"),
|
85 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.3,
|
86 |
+
step=0.05, label="Confidence Threshold"),
|
87 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.6,
|
88 |
+
step=0.05, label="IOU Threshold"),
|
89 |
+
]
|
90 |
+
|
91 |
+
outputs = [gr.Image( type="pil", label="Output Image"),
|
92 |
+
gr.Textbox(label="Result")
|
93 |
+
]
|
94 |
+
|
95 |
+
# examples = [[os.path.join(image_directory, "th (5).jpg"),640, 0.3, 0.6],
|
96 |
+
# [os.path.join(image_directory, "th (8).jpg"),640, 0.3, 0.6],
|
97 |
+
# [os.path.join(image_directory, "th (11).jpg"),640, 0.3, 0.6],
|
98 |
+
# [os.path.join(image_directory, "th (3).jpg"),640, 0.3, 0.6],
|
99 |
+
# [os.path.join(image_directory, "th.jpg"),640, 0.3, 0.6]
|
100 |
+
# ]
|
101 |
+
title = """Corn Deseases Detection Finetuned YOLOv8
|
102 |
+
<br></br>
|
103 |
+
<a href="https://colab.research.google.com/drive/1ittrxr--vJeRqJquZyNfo7dlq6xRADox?authuser=4">
|
104 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Colab" style="display:inline-block;">
|
105 |
+
</a> """
|
106 |
+
description = 'Image Size: Defines the image size for inference.\nConfidence Treshold: Sets the minimum confidence threshold for detections.\nIOU Treshold: Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Useful for reducing duplicates.'
|
107 |
+
|
108 |
+
|
109 |
+
def pil_to_cv2(pil_image):
|
110 |
+
open_cv_image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
111 |
+
return open_cv_image
|
112 |
+
|
113 |
+
|
114 |
+
def process_video(video_path):
|
115 |
+
cap = cv2.VideoCapture(video_path)
|
116 |
+
|
117 |
+
while cap.isOpened():
|
118 |
+
ret, frame = cap.read()
|
119 |
+
if not ret:
|
120 |
+
break
|
121 |
+
|
122 |
+
pil_img = Image.fromarray(frame[..., ::-1])
|
123 |
+
result = model.predict(source=pil_img)
|
124 |
+
for r in result:
|
125 |
+
im_array = r.plot()
|
126 |
+
processed_frame = Image.fromarray(im_array[..., ::-1])
|
127 |
+
yield processed_frame
|
128 |
+
cap.release()
|
129 |
+
|
130 |
+
|
131 |
+
video_iface = gr.Interface(
|
132 |
+
fn=process_video,
|
133 |
+
inputs=[
|
134 |
+
gr.Video(label="Upload Video", interactive=True)
|
135 |
+
],
|
136 |
+
outputs=gr.Image(type="pil",label="Result"),
|
137 |
+
title=title,
|
138 |
+
description="Upload video for inference.",
|
139 |
+
# examples=[[os.path.join(video_directory, "ExampleRifle.mp4")],
|
140 |
+
# [os.path.join(video_directory, "Knife.mp4")],
|
141 |
+
# ]
|
142 |
+
)
|
143 |
+
|
144 |
+
|
145 |
+
image_iface = gr.Interface(fn=response2, inputs=inputs, outputs=outputs,
|
146 |
+
# examples=examples
|
147 |
+
title=title, description=description)
|
148 |
+
|
149 |
+
demo = gr.TabbedInterface([image_iface, video_iface], ["Image Inference", "Video Inference"])
|
150 |
+
|
151 |
+
if __name__ == '__main__':
|
152 |
+
demo.launch()
|