pagling commited on
Commit
2f0b893
·
verified ·
1 Parent(s): 12b5f76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -68
app.py CHANGED
@@ -1,79 +1,43 @@
1
  import gradio as gr
2
- import cv2
3
- import numpy as np
4
- import os
5
- from ultralytics import YOLO
6
 
7
- # 设置上传和结果文件夹
8
- UPLOAD_FOLDER = 'uploads'
9
- RESULT_FOLDER = 'results'
10
- os.makedirs(UPLOAD_FOLDER, exist_ok=True)
11
- os.makedirs(RESULT_FOLDER, exist_ok=True)
12
 
13
- # 加载模型
14
- model = YOLO('yolov8n.pt')
15
 
16
- def process_image(image):
17
- # 保存上传的图像
18
- filename = 'uploaded_image.jpg'
19
- file_path = os.path.join(UPLOAD_FOLDER, filename)
20
- cv2.imwrite(file_path, image)
 
 
 
 
 
21
 
22
- # 处理图像
23
- results = model(image)
24
- detection_results = []
25
- class_counts = {}
26
 
27
- for result in results:
28
- boxes = result.boxes
29
- for box in boxes:
30
- x1, y1, x2, y2 = box.xyxy[0]
31
- conf = box.conf[0]
32
- cls = box.cls[0]
33
- class_name = model.names[int(cls)]
34
 
35
- # 计算每种类别的数量
36
- if class_name in class_counts:
37
- class_counts[class_name] += 1
38
- else:
39
- class_counts[class_name] = 1
40
 
41
- # 绘制边界框和标签
42
- cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
43
- cv2.putText(image, f'{class_name}:{conf:.2f}', (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9,
44
- (36, 255, 12), 2)
45
- detection_results.append(f'Class: {class_name}, Confidence: {conf:.2f}, Box: ({x1}, {y1}), ({x2}, {y2})')
46
-
47
- # 在图像上显示检测到的物体数量信息
48
- y_offset = 30
49
- for class_name, count in class_counts.items():
50
- cv2.putText(image, f'{class_name}: {count}', (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
51
- y_offset += 30
52
-
53
- # 保存处理后的图像
54
- result_filename = 'result_image.jpg'
55
- result_path = os.path.join(RESULT_FOLDER, result_filename)
56
- cv2.imwrite(result_path, image)
57
-
58
- return image, '\n'.join(detection_results)
59
-
60
- # 创建Gradio界面
61
  iface = gr.Interface(
62
- fn=process_image,
63
- inputs=gr.Image(type="numpy", label="上传图像"),
64
- outputs=[gr.Image(type="numpy", label="处理后的图像"), gr.Textbox(label="检测结果")],
65
- title="YOLOv8 图像检测",
66
- description="上传图像并使用YOLOv8模型进行检测"
 
 
 
 
 
 
 
 
67
  )
68
 
69
- # 启动Gradio应用
70
- iface.launch(
71
- share=True,
72
- server_name="0.0.0.0",
73
- server_port=7860,
74
- debug=True,
75
- #auth=("username", "password"),
76
- auth=("test", "12345"),
77
- auth_message="Please enter your credentials to access the app.",
78
- inbrowser=True
79
- )
 
1
  import gradio as gr
2
+ import PIL.Image as Image
3
+ from ultralytics import ASSETS, YOLO
 
 
4
 
5
+ model = YOLO("yolov8n.pt")
 
 
 
 
6
 
 
 
7
 
8
+ def predict_image(img, conf_threshold, iou_threshold):
9
+ """Predicts and plots labeled objects in an image using YOLOv8 model with adjustable confidence and IOU thresholds."""
10
+ results = model.predict(
11
+ source=img,
12
+ conf=conf_threshold,
13
+ iou=iou_threshold,
14
+ show_labels=True,
15
+ show_conf=True,
16
+ imgsz=640,
17
+ )
18
 
19
+ for r in results:
20
+ im_array = r.plot()
21
+ im = Image.fromarray(im_array[..., ::-1])
 
22
 
23
+ return im
 
 
 
 
 
 
24
 
 
 
 
 
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  iface = gr.Interface(
27
+ fn=predict_image,
28
+ inputs=[
29
+ gr.Image(type="pil", label="Upload Image"),
30
+ gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
31
+ gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
32
+ ],
33
+ outputs=gr.Image(type="pil", label="Result"),
34
+ title="Ultralytics Gradio",
35
+ description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",
36
+ examples=[
37
+ [ASSETS / "bus.jpg", 0.25, 0.45],
38
+ [ASSETS / "zidane.jpg", 0.25, 0.45],
39
+ ],
40
  )
41
 
42
+ if __name__ == "__main__":
43
+ iface.launch()