Create visualize.py
Browse files- visualize.py +115 -0
visualize.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ast
|
| 2 |
+
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def draw_border(img, top_left, bottom_right, color=(0, 255, 0), thickness=10, line_length_x=200, line_length_y=200):
|
| 9 |
+
x1, y1 = top_left
|
| 10 |
+
x2, y2 = bottom_right
|
| 11 |
+
|
| 12 |
+
cv2.line(img, (x1, y1), (x1, y1 + line_length_y), color, thickness) #-- top-left
|
| 13 |
+
cv2.line(img, (x1, y1), (x1 + line_length_x, y1), color, thickness)
|
| 14 |
+
|
| 15 |
+
cv2.line(img, (x1, y2), (x1, y2 - line_length_y), color, thickness) #-- bottom-left
|
| 16 |
+
cv2.line(img, (x1, y2), (x1 + line_length_x, y2), color, thickness)
|
| 17 |
+
|
| 18 |
+
cv2.line(img, (x2, y1), (x2 - line_length_x, y1), color, thickness) #-- top-right
|
| 19 |
+
cv2.line(img, (x2, y1), (x2, y1 + line_length_y), color, thickness)
|
| 20 |
+
|
| 21 |
+
cv2.line(img, (x2, y2), (x2, y2 - line_length_y), color, thickness) #-- bottom-right
|
| 22 |
+
cv2.line(img, (x2, y2), (x2 - line_length_x, y2), color, thickness)
|
| 23 |
+
|
| 24 |
+
return img
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
results = pd.read_csv('./test_interpolated.csv')
|
| 28 |
+
|
| 29 |
+
# load video
|
| 30 |
+
video_path = 'sample.mp4'
|
| 31 |
+
cap = cv2.VideoCapture(video_path)
|
| 32 |
+
|
| 33 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Specify the codec
|
| 34 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 35 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 36 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 37 |
+
out = cv2.VideoWriter('./out.mp4', fourcc, fps, (width, height))
|
| 38 |
+
|
| 39 |
+
license_plate = {}
|
| 40 |
+
for car_id in np.unique(results['car_id']):
|
| 41 |
+
max_ = np.amax(results[results['car_id'] == car_id]['license_number_score'])
|
| 42 |
+
license_plate[car_id] = {'license_crop': None,
|
| 43 |
+
'license_plate_number': results[(results['car_id'] == car_id) &
|
| 44 |
+
(results['license_number_score'] == max_)]['license_number'].iloc[0]}
|
| 45 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, results[(results['car_id'] == car_id) &
|
| 46 |
+
(results['license_number_score'] == max_)]['frame_nmr'].iloc[0])
|
| 47 |
+
ret, frame = cap.read()
|
| 48 |
+
|
| 49 |
+
x1, y1, x2, y2 = ast.literal_eval(results[(results['car_id'] == car_id) &
|
| 50 |
+
(results['license_number_score'] == max_)]['license_plate_bbox'].iloc[0].replace('[ ', '[').replace(' ', ' ').replace(' ', ' ').replace(' ', ','))
|
| 51 |
+
|
| 52 |
+
license_crop = frame[int(y1):int(y2), int(x1):int(x2), :]
|
| 53 |
+
license_crop = cv2.resize(license_crop, (int((x2 - x1) * 400 / (y2 - y1)), 400))
|
| 54 |
+
|
| 55 |
+
license_plate[car_id]['license_crop'] = license_crop
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
frame_nmr = -1
|
| 59 |
+
|
| 60 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
| 61 |
+
|
| 62 |
+
# read frames
|
| 63 |
+
ret = True
|
| 64 |
+
while ret:
|
| 65 |
+
ret, frame = cap.read()
|
| 66 |
+
frame_nmr += 1
|
| 67 |
+
if ret:
|
| 68 |
+
df_ = results[results['frame_nmr'] == frame_nmr]
|
| 69 |
+
for row_indx in range(len(df_)):
|
| 70 |
+
# draw car
|
| 71 |
+
car_x1, car_y1, car_x2, car_y2 = ast.literal_eval(df_.iloc[row_indx]['car_bbox'].replace('[ ', '[').replace(' ', ' ').replace(' ', ' ').replace(' ', ','))
|
| 72 |
+
draw_border(frame, (int(car_x1), int(car_y1)), (int(car_x2), int(car_y2)), (0, 255, 0), 25,
|
| 73 |
+
line_length_x=200, line_length_y=200)
|
| 74 |
+
|
| 75 |
+
# draw license plate
|
| 76 |
+
x1, y1, x2, y2 = ast.literal_eval(df_.iloc[row_indx]['license_plate_bbox'].replace('[ ', '[').replace(' ', ' ').replace(' ', ' ').replace(' ', ','))
|
| 77 |
+
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 12)
|
| 78 |
+
|
| 79 |
+
# crop license plate
|
| 80 |
+
license_crop = license_plate[df_.iloc[row_indx]['car_id']]['license_crop']
|
| 81 |
+
|
| 82 |
+
H, W, _ = license_crop.shape
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
frame[int(car_y1) - H - 100:int(car_y1) - 100,
|
| 86 |
+
int((car_x2 + car_x1 - W) / 2):int((car_x2 + car_x1 + W) / 2), :] = license_crop
|
| 87 |
+
|
| 88 |
+
frame[int(car_y1) - H - 400:int(car_y1) - H - 100,
|
| 89 |
+
int((car_x2 + car_x1 - W) / 2):int((car_x2 + car_x1 + W) / 2), :] = (255, 255, 255)
|
| 90 |
+
|
| 91 |
+
(text_width, text_height), _ = cv2.getTextSize(
|
| 92 |
+
license_plate[df_.iloc[row_indx]['car_id']]['license_plate_number'],
|
| 93 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 94 |
+
4.3,
|
| 95 |
+
17)
|
| 96 |
+
|
| 97 |
+
cv2.putText(frame,
|
| 98 |
+
license_plate[df_.iloc[row_indx]['car_id']]['license_plate_number'],
|
| 99 |
+
(int((car_x2 + car_x1 - text_width) / 2), int(car_y1 - H - 250 + (text_height / 2))),
|
| 100 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 101 |
+
4.3,
|
| 102 |
+
(0, 0, 0),
|
| 103 |
+
17)
|
| 104 |
+
|
| 105 |
+
except:
|
| 106 |
+
pass
|
| 107 |
+
|
| 108 |
+
out.write(frame)
|
| 109 |
+
frame = cv2.resize(frame, (1280, 720))
|
| 110 |
+
|
| 111 |
+
# cv2.imshow('frame', frame)
|
| 112 |
+
# cv2.waitKey(0)
|
| 113 |
+
|
| 114 |
+
out.release()
|
| 115 |
+
cap.release()
|