xangcastle commited on
Commit
d79bac4
·
1 Parent(s): 3f7b316

activate gpu

Browse files
Files changed (2) hide show
  1. app.py +8 -6
  2. detector/utils.py +3 -4
app.py CHANGED
@@ -63,6 +63,8 @@ def fn_video(video, initial_time, duration):
63
  while cap.isOpened():
64
  try:
65
  ret, frame = cap.read()
 
 
66
  if not ret:
67
  break
68
  frame_copy = frame.copy()
@@ -72,7 +74,7 @@ def fn_video(video, initial_time, duration):
72
  if num_frames < min_frame:
73
  num_frames += 1
74
  continue
75
- yolo_detections = detect_plates(frame)
76
  detections = yolo_to_norfair(yolo_detections)
77
  tracked_objects = tracker.update(detections=detections)
78
  for obj in tracked_objects:
@@ -80,22 +82,22 @@ def fn_video(video, initial_time, duration):
80
  bbox = obj.last_detection.points
81
  bbox = int(bbox[0][0]), int(bbox[0][1]), int(bbox[1][0]), int(bbox[1][1])
82
  if obj.id not in plates.keys():
83
- crop = imcrop(frame, bbox)
84
  text = detect_chars(crop)
85
  plates[obj.id] = text
86
  thread = Thread(target=send_request, args=(frame_copy, text, bbox))
87
  thread.start()
88
 
89
  cv2.rectangle(
90
- frame,
91
  (bbox[0], bbox[1]),
92
  (bbox[2], bbox[3]),
93
  (0, 255, 0),
94
  2,
95
  )
96
- draw_text(frame, plates[obj.id], (bbox[0], bbox[1]))
97
  cv2.putText(
98
- frame,
99
  plates[obj.id],
100
  (bbox[0], bbox[1]),
101
  cv2.FONT_HERSHEY_SIMPLEX,
@@ -103,7 +105,7 @@ def fn_video(video, initial_time, duration):
103
  (0, 255, 0),
104
  2,
105
  )
106
- final_video.write(frame)
107
  num_frames += 1
108
  if num_frames == max_frame:
109
  break
 
63
  while cap.isOpened():
64
  try:
65
  ret, frame = cap.read()
66
+ gpu_frame = cv2.cuda_GpuMat()
67
+ gpu_frame.upload(frame)
68
  if not ret:
69
  break
70
  frame_copy = frame.copy()
 
74
  if num_frames < min_frame:
75
  num_frames += 1
76
  continue
77
+ yolo_detections = detect_plates(gpu_frame)
78
  detections = yolo_to_norfair(yolo_detections)
79
  tracked_objects = tracker.update(detections=detections)
80
  for obj in tracked_objects:
 
82
  bbox = obj.last_detection.points
83
  bbox = int(bbox[0][0]), int(bbox[0][1]), int(bbox[1][0]), int(bbox[1][1])
84
  if obj.id not in plates.keys():
85
+ crop = imcrop(gpu_frame, bbox)
86
  text = detect_chars(crop)
87
  plates[obj.id] = text
88
  thread = Thread(target=send_request, args=(frame_copy, text, bbox))
89
  thread.start()
90
 
91
  cv2.rectangle(
92
+ gpu_frame,
93
  (bbox[0], bbox[1]),
94
  (bbox[2], bbox[3]),
95
  (0, 255, 0),
96
  2,
97
  )
98
+ draw_text(gpu_frame, plates[obj.id], (bbox[0], bbox[1]))
99
  cv2.putText(
100
+ gpu_frame,
101
  plates[obj.id],
102
  (bbox[0], bbox[1]),
103
  cv2.FONT_HERSHEY_SIMPLEX,
 
105
  (0, 255, 0),
106
  2,
107
  )
108
+ final_video.write(gpu_frame)
109
  num_frames += 1
110
  if num_frames == max_frame:
111
  break
detector/utils.py CHANGED
@@ -9,14 +9,13 @@ import requests
9
  BASE_DIR = os.path.abspath(os.getcwd())
10
 
11
  device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
12
- cpu = torch.device('cpu')
13
- print('Loading models...', cpu)
14
 
15
  model_plates = torch.hub.load('ultralytics/yolov5', 'custom',
16
- path=os.path.join(BASE_DIR, 'detector', 'static', 'plates.pt'), device=cpu)
17
 
18
  model_chars = torch.hub.load('ultralytics/yolov5', 'custom',
19
- path=os.path.join(BASE_DIR, 'detector', 'static', 'chars.pt'), device=cpu)
20
 
21
 
22
  def pad_img_to_fit_bbox(img, x1, x2, y1, y2):
 
9
  BASE_DIR = os.path.abspath(os.getcwd())
10
 
11
  device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
12
+ print('Loading models...', device)
 
13
 
14
  model_plates = torch.hub.load('ultralytics/yolov5', 'custom',
15
+ path=os.path.join(BASE_DIR, 'detector', 'static', 'plates.pt'), device=device)
16
 
17
  model_chars = torch.hub.load('ultralytics/yolov5', 'custom',
18
+ path=os.path.join(BASE_DIR, 'detector', 'static', 'chars.pt'), device=device)
19
 
20
 
21
  def pad_img_to_fit_bbox(img, x1, x2, y1, y2):