-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
144 lines (129 loc) · 4.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import cv2
import time
import json
from os import chdir, getcwd, listdir, path, mkdir
from utils.config import CarPredictionConfig, MaskRCNN
from utils.functions import adapt_image, prediction
import warnings
warnings.filterwarnings("ignore")
def play_video(filename):
cap = cv2.VideoCapture("videos/" + filename)
while True:
success, img = cap.read()
if success==True:
img = cv2.resize(img, (960,540))
cv2.imshow("Video", img)
cv2.waitKey(20)
else:
break
def save_video(filename):
# Spliting the video into frames
cap = cv2.VideoCapture("videos/" + filename)
i = 0
Path = "frames/" + filename + "/"
chdir("frames")
mkdir(f"{filename}")
chdir(f"{filename}")
print(getcwd())
if cap.isOpened():
while True:
success, img = cap.read()
if success==True:
img = cv2.resize(img, (960,540))
file = path.join(Path , f"{i}.jpg")
print(file)
cv2.imwrite(f"{i}.jpg", img)
print("created")
i+=1
else:
break
chdir(f"../..")
cv2.destroyAllWindows()
def show_pred_video(filename):
Path = "frames/" + filename
# Open Json with predictions
with open(Path + "/pred_boxes.json", 'r') as f:
boxes = json.load(f)
print("json loaded")
print(len(boxes))
# AQUI HAY QUE MONTAR UNA FUNCION BONITA
ptime = 0
sorted_images = sorted([name for name in listdir(Path) if ".jpg" in name])
fr_num = len(sorted_images)
print(fr_num)
#for i in range(fr_num):
i = 0
while i<fr_num:
img = cv2.imread(Path + f"/{i}.jpg")
# Put FPS Counter
#ctime = time.time()
#fps = 1/(ctime - ptime)
#ptime = ctime
#cv2.putText(img, f"FPS: {int(fps)}", (850,50), cv2.FONT_HERSHEY_PLAIN,
# 1.5, (0,255,0),2)
# ploting prediction
# plot_video_pred(img, sample, model)
try:
for box in boxes[i]:
x1, y1, x2, y2 = box
cv2.rectangle(img,(x1, y1), (x2, y2), (0,255,0), 2)
print(boxes[i])
print("rect" + "%"*20)
cv2.waitKey(1)
except:
pass
cv2.imshow("Prediction", img)
cv2.waitKey(20)
i+=1
print(i)
cv2.destroyAllWindows()
def pred_video(filename):
path = "frames/" + filename
# Loading prediction model
predconfig = CarPredictionConfig()
model = MaskRCNN(mode="inference", model_dir="model/pred/", config=predconfig)
model.load_weights("model/mask_rcnn_car_0009.h5", by_name=True)
print("model loaded "+"%"*20)
pred_list = []
# Percentage for data predicted
sorted_images = sorted([name for name in listdir(path) if ".jpg" in name])
per100 = len(sorted_images)
p = 1
for i in range(per100):
img = cv2.imread(path + f"/{i}.jpg")
sample = adapt_image(img, predconfig)
boxes = prediction(sample, model)
pred_list.append(boxes)
print("predicting... " + str(int(100*p/per100)) + " %")
p+=1
with open(path + "/pred_boxes.json", 'w') as f:
json.dump(pred_list, f, indent=2)
return pred_list
def pred_image(filename):
Path = "images/"
# Loading prediction model
predconfig = CarPredictionConfig()
model = MaskRCNN(mode="inference", model_dir="model/pred/", config=predconfig)
model.load_weights("model/mask_rcnn_car_0009.h5", by_name=True)
# predicting boxes
img = cv2.imread(Path + filename)
img = cv2.resize(img, (960,540))
sample = adapt_image(img, predconfig)
boxes = prediction(sample, model)
cv2.destroyAllWindows()
try:
for box in boxes:
x1, y1, x2, y2 = box
cv2.rectangle(img,(x1, y1), (x2, y2), (0,255,0), 2)
print("rect" + "%"*20)
except:
pass
cv2.imshow("Prediction", img)
if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
def display_image(filename):
Path = "images/"
img = cv2.imread(Path + filename)
img = cv2.resize(img, (960,540))
cv2.imshow(f"{filename}", img)
cv2.waitKey(0)