linhcuem commited on
Commit
31dad98
1 Parent(s): 5ff22f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -62
app.py CHANGED
@@ -32,7 +32,7 @@ image_path = [['test_images/2a998cfb0901db5f8210.jpg','cham_diem_yolov8', 640, 0
32
  # model = YOLO('linhcuem/chamdiemgianhang_yolov8_ver21')
33
  # model = YOLO('linhcuem/cham_diem_yolov8_ver20')
34
  # model_ids = ['linhcuem/checker_TB_yolov8_ver1', 'linhcuem/cham_diem_yolov8', 'linhcuem/chamdiemgianhang_yolov8_300epochs', 'linhcuem/cham_diem_yolov8_ver20', 'linhcuem/chamdiemgianhang_yolov8_ver21']
35
- # # model = YOLO('linhcuem/checker_TB_yolov8_ver1')
36
  # current_model_id = model_ids[-1]
37
  # model = YOLO(current_model_id)
38
  # model = YOLO(model_path)
@@ -46,7 +46,7 @@ def yolov8_img_inference(
46
  iou_threshold: gr.inputs.Slider = 0.45,
47
  ):
48
  # model = YOLO(model_path)
49
- model = YOLO(model_path)
50
  model.conf = conf_threshold
51
  model.iou = iou_threshold
52
  # model.overrides['conf'] = conf_threshold
@@ -78,43 +78,43 @@ def yolov8_img_inference(
78
  return render, {names[k]: v for k, v in present_objects.items()}
79
 
80
 
81
- # def yolov8_vid_inference(video_path):
82
- # cap = cv2.VideoCapture(video_path)
83
- # while cap.isOpened():
84
- # success, frame = cap.read()
85
-
86
- # if success:
87
- # frame_copy = frame.copy()
88
- # outputs = model.predict(source=frame)
89
- # results = outputs[0].cpu().numpy()
90
- # for i, det in enumerate(results.boxes.xyxy):
91
- # cv2.rectangle(
92
- # frame_copy,
93
- # (int(det[0]), int(det[1])),
94
- # (int(det[2]), int(det[3])),
95
- # color=(0, 0, 255),
96
- # thickness=2,
97
- # lineType=cv2.LINE_AA
98
- # )
99
- # yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
100
 
101
 
102
- # inputs_vid = [
103
- # gr.components.Video(type="filepath", label="Input Video"),
104
- # ]
105
 
106
- # outputs_vid = [
107
- # gr.components.Image(type="numpy", label="Output Image"),
108
- # ]
109
 
110
- # interface_vid = gr.Interface(
111
- # fn=yolov8_vid_inference,
112
- # inputs = inputs_vid,
113
- # outputs = outputs_vid,
114
- # title = "Detect Thiên Việt productions",
115
- # cache_examples = False,
116
 
117
- # )
118
 
119
  inputs = [
120
  gr.inputs.Image(type="filepath", label="Input Image"),
@@ -130,37 +130,37 @@ outputs =gr.outputs.Image(type="filepath", label="Output Image")
130
 
131
  title = "Detect Thiên Việt productions"
132
 
133
- # interface_image = gr.Interface(
134
- # fn=yolov8_img_inference,
135
- # inputs=[
136
- # gr.Image(type='pil'),
137
- # gr.Dropdown(["linhcuem/checker_TB_yolov8_ver1", "linhcuem/chamdiemgianhang_yolov8_ver21"],
138
- # default="linhcuem/checker_TB_yolov8_ver1", label="Model"),
139
- # gr.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
140
- # gr.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
141
- # gr.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
142
- # ],
143
- # outputs=[gr.Image(type="pil"),gr.Textbox(show_label=False)],
144
- # title=title,
145
- # examples=image_path,
146
- # cache_examples=True if image_path else False,
147
-
148
- # )
149
-
150
- # gr.TabbedInterface(
151
- # [interface_image, interface_vid],
152
- # tab_names=['Image inference', 'Video inference']
153
- # ).queue().launch()
154
-
155
- demo_app = gr.Interface(
156
  fn=yolov8_img_inference,
157
- inputs=inputs,
158
- outputs=outputs,
 
 
 
 
 
 
 
159
  title=title,
160
  examples=image_path,
161
- cache_examples=True,
162
- theme='huggingface',
163
  )
164
- demo_app.launch(debug=True, enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
  # interface_image.launch(debug=True, enable_queue=True)
 
32
  # model = YOLO('linhcuem/chamdiemgianhang_yolov8_ver21')
33
  # model = YOLO('linhcuem/cham_diem_yolov8_ver20')
34
  # model_ids = ['linhcuem/checker_TB_yolov8_ver1', 'linhcuem/cham_diem_yolov8', 'linhcuem/chamdiemgianhang_yolov8_300epochs', 'linhcuem/cham_diem_yolov8_ver20', 'linhcuem/chamdiemgianhang_yolov8_ver21']
35
+ model = YOLO('linhcuem/checker_TB_yolov8_ver1')
36
  # current_model_id = model_ids[-1]
37
  # model = YOLO(current_model_id)
38
  # model = YOLO(model_path)
 
46
  iou_threshold: gr.inputs.Slider = 0.45,
47
  ):
48
  # model = YOLO(model_path)
49
+
50
  model.conf = conf_threshold
51
  model.iou = iou_threshold
52
  # model.overrides['conf'] = conf_threshold
 
78
  return render, {names[k]: v for k, v in present_objects.items()}
79
 
80
 
81
+ def yolov8_vid_inference(video_path):
82
+ cap = cv2.VideoCapture(video_path)
83
+ while cap.isOpened():
84
+ success, frame = cap.read()
85
+
86
+ if success:
87
+ frame_copy = frame.copy()
88
+ outputs = model.predict(source=frame)
89
+ results = outputs[0].cpu().numpy()
90
+ for i, det in enumerate(results.boxes.xyxy):
91
+ cv2.rectangle(
92
+ frame_copy,
93
+ (int(det[0]), int(det[1])),
94
+ (int(det[2]), int(det[3])),
95
+ color=(0, 0, 255),
96
+ thickness=2,
97
+ lineType=cv2.LINE_AA
98
+ )
99
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
100
 
101
 
102
+ inputs_vid = [
103
+ gr.components.Video(type="filepath", label="Input Video"),
104
+ ]
105
 
106
+ outputs_vid = [
107
+ gr.components.Image(type="numpy", label="Output Image"),
108
+ ]
109
 
110
+ interface_vid = gr.Interface(
111
+ fn=yolov8_vid_inference,
112
+ inputs = inputs_vid,
113
+ outputs = outputs_vid,
114
+ title = "Detect Thiên Việt productions",
115
+ cache_examples = False,
116
 
117
+ )
118
 
119
  inputs = [
120
  gr.inputs.Image(type="filepath", label="Input Image"),
 
130
 
131
  title = "Detect Thiên Việt productions"
132
 
133
+ interface_image = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  fn=yolov8_img_inference,
135
+ inputs=[
136
+ gr.Image(type='pil'),
137
+ gr.Dropdown(["linhcuem/checker_TB_yolov8_ver1"],
138
+ default="linhcuem/checker_TB_yolov8_ver1", label="Model"),
139
+ gr.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
140
+ gr.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
141
+ gr.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
142
+ ],
143
+ outputs=[gr.Image(type="pil"),gr.Textbox(show_label=False)],
144
  title=title,
145
  examples=image_path,
146
+ cache_examples=True if image_path else False,
147
+
148
  )
149
+
150
+ gr.TabbedInterface(
151
+ [interface_image, interface_vid],
152
+ tab_names=['Image inference', 'Video inference']
153
+ ).queue().launch()
154
+
155
+ # demo_app = gr.Interface(
156
+ # fn=yolov8_img_inference,
157
+ # inputs=inputs,
158
+ # outputs=outputs,
159
+ # title=title,
160
+ # examples=image_path,
161
+ # cache_examples=True,
162
+ # theme='huggingface',
163
+ # )
164
+ # demo_app.launch(debug=True, enable_queue=True)
165
 
166
  # interface_image.launch(debug=True, enable_queue=True)