yan123yan commited on
Commit
7078129
1 Parent(s): 3ce0474

maintain yolov5 code

Browse files
app.py CHANGED
@@ -38,8 +38,8 @@ def inference(image, model_name, conf_threshold, iou_threshold):
38
  if model_name == "YOLO-V7":
39
  return get_yolov7_result(image, conf_threshold, iou_threshold, label_names)
40
  elif model_name == "YOLO-V5":
41
- return get_yolov5_result(image, conf_threshold, iou_threshold, label_names)
42
- #return None, None
43
  elif model_name == "YOLO-V8":
44
  return get_yolov8_result(image, conf_threshold, iou_threshold, label_names)
45
  else:
 
38
  if model_name == "YOLO-V7":
39
  return get_yolov7_result(image, conf_threshold, iou_threshold, label_names)
40
  elif model_name == "YOLO-V5":
41
+ #return get_yolov5_result(image, conf_threshold, iou_threshold, label_names)
42
+ return None, None
43
  elif model_name == "YOLO-V8":
44
  return get_yolov8_result(image, conf_threshold, iou_threshold, label_names)
45
  else:
infer/yolov5/get_results.py CHANGED
@@ -4,12 +4,26 @@ import numpy as np
4
  import streamlit as st
5
  import os
6
  import shutil
 
 
7
 
8
  from infer.yolov5 import detect
9
 
10
  colors = [(np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) for _ in range(20)]
11
 
12
  def get_yolov5_result(image, conf_threshold, iou_threshold, class_names):
 
 
 
 
 
 
 
 
 
 
 
 
13
  weights = 'my_model/v5-n.pt'
14
  device = 'cpu'
15
  #model = torch.hub.load('ultralytics/yolov5', 'custom', path=weights, force_reload=True)
@@ -45,6 +59,7 @@ def get_yolov5_result(image, conf_threshold, iou_threshold, class_names):
45
  #
46
 
47
  result_list = []
 
48
  with open('temp/labels/temp.txt', "r") as f:
49
  lines = f.readlines()
50
  for line in lines:
@@ -73,6 +88,16 @@ def get_yolov5_result(image, conf_threshold, iou_threshold, class_names):
73
 
74
  remove_yolov5_temp_folders()
75
 
 
 
 
 
 
 
 
 
 
 
76
  return img0, result_list
77
 
78
  def remove_yolov5_temp_folders():
 
4
  import streamlit as st
5
  import os
6
  import shutil
7
+ import sys
8
+ import platform
9
 
10
  from infer.yolov5 import detect
11
 
12
  colors = [(np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) for _ in range(20)]
13
 
14
  def get_yolov5_result(image, conf_threshold, iou_threshold, class_names):
15
+
16
+ os_name = platform.system()
17
+ if os_name == 'Windows':
18
+ sys.path.insert(0, "infer\yolov5")
19
+ elif os_name == 'Linux':
20
+ sys.path.insert(0, "/home/user/app/infer/yolov5")
21
+ else:
22
+ return "Unknown"
23
+
24
+ if os.path.exists('temp/'):
25
+ shutil.rmtree('temp/', ignore_errors=True)
26
+
27
  weights = 'my_model/v5-n.pt'
28
  device = 'cpu'
29
  #model = torch.hub.load('ultralytics/yolov5', 'custom', path=weights, force_reload=True)
 
59
  #
60
 
61
  result_list = []
62
+
63
  with open('temp/labels/temp.txt', "r") as f:
64
  lines = f.readlines()
65
  for line in lines:
 
88
 
89
  remove_yolov5_temp_folders()
90
 
91
+ os_name = platform.system()
92
+ if os_name == 'Windows':
93
+ if "infer\yolov5" in sys.path:
94
+ sys.path.remove("infer\yolov5")
95
+ elif os_name == 'Linux':
96
+ if "/home/user/app/infer/yolov5" in sys.path:
97
+ sys.path.remove("/home/user/app/infer/yolov5")
98
+ else:
99
+ return "Unknown"
100
+
101
  return img0, result_list
102
 
103
  def remove_yolov5_temp_folders():
infer/yolov5/models/common.py CHANGED
@@ -348,6 +348,17 @@ class DetectMultiBackend(nn.Module):
348
  w = attempt_download(w) # download if not local
349
 
350
  if pt: # PyTorch
 
 
 
 
 
 
 
 
 
 
 
351
  model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
352
  stride = max(int(model.stride.max()), 32) # model stride
353
  names = model.module.names if hasattr(model, 'module') else model.names # get class names
 
348
  w = attempt_download(w) # download if not local
349
 
350
  if pt: # PyTorch
351
+
352
+ import platform
353
+ import sys
354
+ os_name = platform.system()
355
+ if os_name == 'Windows':
356
+ sys.path.insert(0, "infer\yolov5")
357
+ elif os_name == 'Linux':
358
+ sys.path.insert(0, "/home/user/app/infer/yolov5")
359
+ else:
360
+ return "Unknown"
361
+
362
  model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
363
  stride = max(int(model.stride.max()), 32) # model stride
364
  names = model.module.names if hasattr(model, 'module') else model.names # get class names
infer/yolov5/models/experimental.py CHANGED
@@ -72,7 +72,7 @@ class Ensemble(nn.ModuleList):
72
 
73
  def attempt_load(weights, device=None, inplace=True, fuse=True):
74
  # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
75
- from models.yolo import Detect, Model
76
 
77
  model = Ensemble()
78
  for w in weights if isinstance(weights, list) else [weights]:
 
72
 
73
  def attempt_load(weights, device=None, inplace=True, fuse=True):
74
  # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
75
+ from infer.yolov5.models.yolo import Detect, Model
76
 
77
  model = Ensemble()
78
  for w in weights if isinstance(weights, list) else [weights]:
infer/yolov5/models/tf.py CHANGED
@@ -28,7 +28,7 @@ import torch.nn as nn
28
  from tensorflow import keras
29
 
30
  from infer.yolov5.models.common import (C3, SPP, SPPF, Bottleneck, BottleneckCSP, C3x, Concat, Conv, CrossConv, DWConv,
31
- DWConvTranspose2d, Focus, autopad)
32
  from infer.yolov5.models.experimental import MixConv2d, attempt_load
33
  from infer.yolov5.models.yolo import Detect, Segment
34
  from infer.yolov5.utils.activations import SiLU
 
28
  from tensorflow import keras
29
 
30
  from infer.yolov5.models.common import (C3, SPP, SPPF, Bottleneck, BottleneckCSP, C3x, Concat, Conv, CrossConv, DWConv,
31
+ DWConvTranspose2d, Focus, autopad)
32
  from infer.yolov5.models.experimental import MixConv2d, attempt_load
33
  from infer.yolov5.models.yolo import Detect, Segment
34
  from infer.yolov5.utils.activations import SiLU
infer/yolov7/get_results.py CHANGED
@@ -7,6 +7,8 @@ from infer.yolov7.utils.torch_utils import select_device
7
  import numpy as np
8
  import os
9
  from huggingface_hub import Repository
 
 
10
 
11
  colors = [(np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) for _ in range(20)]
12
 
@@ -53,4 +55,17 @@ def get_yolov7_result(image, conf_threshold, iou_threshold, class_names):
53
  cv2.rectangle(img0, (x1, y1), (x2, y2), color, 2)
54
  cv2.putText(img0, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
55
  img0 = img0[...,::-1]
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  return img0, result_list
 
7
  import numpy as np
8
  import os
9
  from huggingface_hub import Repository
10
+ import sys
11
+ import platform
12
 
13
  colors = [(np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) for _ in range(20)]
14
 
 
55
  cv2.rectangle(img0, (x1, y1), (x2, y2), color, 2)
56
  cv2.putText(img0, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
57
  img0 = img0[...,::-1]
58
+
59
+ os_name = platform.system()
60
+ if os_name == 'Windows':
61
+ if "infer\yolov7" in sys.path:
62
+ sys.path.remove("infer\yolov7")
63
+ # print(sys.path)
64
+ elif os_name == 'Linux':
65
+ if "/home/user/app/infer/yolov7" in sys.path:
66
+ sys.path.remove("/home/user/app/infer/yolov7")
67
+ else:
68
+ return "Unknown"
69
+
70
+
71
  return img0, result_list
infer/yolov7/models/experimental.py CHANGED
@@ -263,7 +263,7 @@ def attempt_load(weights, map_location=None):
263
  sys.path.insert(0, "/home/user/app/infer/yolov7")
264
  else:
265
  return "Unknown"
266
-
267
  #sys.path.insert(0, "/home/user/app/infer/yolov7")
268
  ckpt = torch.load(w, map_location=map_location) # load
269
  model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model
 
263
  sys.path.insert(0, "/home/user/app/infer/yolov7")
264
  else:
265
  return "Unknown"
266
+ #print(sys.path)
267
  #sys.path.insert(0, "/home/user/app/infer/yolov7")
268
  ckpt = torch.load(w, map_location=map_location) # load
269
  model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model