10
10
from absl .flags import FLAGS
11
11
import core .utils as utils
12
12
from core .yolov4 import filter_boxes
13
- from core .functions import count_objects
13
+ from core .functions import *
14
14
from tensorflow .python .saved_model import tag_constants
15
15
from PIL import Image
16
16
import cv2
32
32
flags .DEFINE_boolean ('count' , False , 'count objects within video' )
33
33
flags .DEFINE_boolean ('dont_show' , False , 'dont show video output' )
34
34
flags .DEFINE_boolean ('info' , False , 'print info on detections' )
35
+ flags .DEFINE_boolean ('crop' , False , 'crop detections from images' )
35
36
36
37
def main (_argv ):
37
38
config = ConfigProto ()
@@ -40,7 +41,9 @@ def main(_argv):
40
41
STRIDES , ANCHORS , NUM_CLASS , XYSCALE = utils .load_config (FLAGS )
41
42
input_size = FLAGS .size
42
43
video_path = FLAGS .video
43
-
44
+ # get video name by using split method
45
+ video_name = video_path .split ('/' )[- 1 ]
46
+ video_name = video_name .split ('.' )[0 ]
44
47
if FLAGS .framework == 'tflite' :
45
48
interpreter = tf .lite .Interpreter (model_path = FLAGS .weights )
46
49
interpreter .allocate_tensors ()
@@ -68,10 +71,12 @@ def main(_argv):
68
71
codec = cv2 .VideoWriter_fourcc (* FLAGS .output_format )
69
72
out = cv2 .VideoWriter (FLAGS .output , codec , fps , (width , height ))
70
73
74
+ frame_num = 0
71
75
while True :
72
76
return_value , frame = vid .read ()
73
77
if return_value :
74
78
frame = cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB )
79
+ frame_num += 1
75
80
image = Image .fromarray (frame )
76
81
else :
77
82
print ('Video has ended or failed, try a different video format!' )
@@ -116,6 +121,24 @@ def main(_argv):
116
121
117
122
pred_bbox = [bboxes , scores .numpy ()[0 ], classes .numpy ()[0 ], valid_detections .numpy ()[0 ]]
118
123
124
+ # if crop flag is enabled, crop each detection and save it as new image
125
+ if FLAGS .crop :
126
+ crop_rate = 150 # capture images every so many frames (ex. crop photos every 150 frames)
127
+ crop_path = os .path .join (os .getcwd (), 'detections' , 'crop' , video_name )
128
+ try :
129
+ os .mkdir (crop_path )
130
+ except FileExistsError :
131
+ pass
132
+ if frame_num % crop_rate == 0 :
133
+ final_path = os .path .join (crop_path , 'frame_' + str (frame_num ))
134
+ try :
135
+ os .mkdir (final_path )
136
+ except FileExistsError :
137
+ pass
138
+ crop_objects (cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB ), pred_bbox , final_path )
139
+ else :
140
+ pass
141
+
119
142
if FLAGS .count :
120
143
# count objects found
121
144
counted_classes = count_objects (pred_bbox , by_class = False )
0 commit comments