Skip to content

Commit

Permalink
Adding code after remove one commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aagustinconti committed Nov 22, 2022
1 parent f994df0 commit f4b2432
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
18 changes: 14 additions & 4 deletions test_oop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,35 @@
test = YoloSortCount()

# Source
test.video_path = 0 #"https://www.youtube.com/watch?v=yhkbg8p2Gts"
test.video_path = 0#"https://www.youtube.com/watch?v=qP1y7Tdab7Y" | "http://IP/hls/stream_src.m3u8" | 0 | "img_bank/cows_for_sale.mp4"

test.max_fps = 1000 #Max 1000
test.max_width = 720

# Show results
test.show_img = True
test.hold_img = False

test.auto_load_roi = True

test.ends_in_sec = 10

# Debug
test.show_configs = False
test.show_detection = False
test.show_tracking = False
test.show_count = True
test.show_count = False

# Detection model
test.class_ids = []
test.class_ids = [0]
test.conf_thres = 0.5

# Frame
test.inv_h_frame = True
test.inv_h_frame = False

# Save
test.save_loc = "results/test_test"
test.save_vid = True

# Run
test.run()
52 changes: 37 additions & 15 deletions yolov7_sort_count_oop.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def __init__(self):
self.ds_nn_budget = 100
self.ds_color = (0, 0, 255)

self.show_configs = False
self.max_fps = 25
self.max_width = 720


# Pre defined
self.names = None
Expand All @@ -94,6 +96,7 @@ def __init__(self):
logging.basicConfig(
format='%(asctime)s | %(levelname)s: %(message)s', level=logging.NOTSET)

self.show_configs = False
self.show_detection = False
self.show_tracking = False
self.show_count = False
Expand Down Expand Up @@ -145,8 +148,21 @@ def load_video_capture(self, video_path):

orig_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
orig_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

orig_ratio = orig_h / orig_w

if orig_w > self.max_width:
logging.info('Capture has more width than max. width allowed. Rezising...')
cap = self.change_res(cap, self.max_width, orig_ratio)

orig_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
orig_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

logging.info(f'Capture has been resized to {(orig_w,orig_h)}')

orig_fps = cap.get(cv2.CAP_PROP_FPS) % 100


return cap, orig_w, orig_h, orig_fps

except Exception as err:
Expand All @@ -157,7 +173,7 @@ def load_save_vid(self, save_loc, orig_w, orig_h):

try:

result = cv2.VideoWriter(save_loc+'.avi',
result = cv2.VideoWriter(save_loc+'.mp4',
cv2.VideoWriter_fourcc(*'MJPG'),
10, (orig_w, orig_h))
return result
Expand Down Expand Up @@ -204,16 +220,16 @@ def load_roi(self):
cap_roi, _, _, _ = self.load_video_capture(self.video_path)
ret, select_roi_frame = cap_roi.read()

frame_count_roi = 0
while frame_count_roi <= 3 and ret:

ret, select_roi_frame = cap_roi.read()
if not self.hold_img:
frame_count_roi = 0
while frame_count_roi <= 3 and ret:

# To show image correctly (IE: web camera)
if self.inv_h_frame:
select_roi_frame = cv2.flip(select_roi_frame, 1)

frame_count_roi += 1
ret, select_roi_frame = cap_roi.read()
frame_count_roi += 1

# To show image correctly (IE: web camera)
if self.inv_h_frame:
select_roi_frame = cv2.flip(select_roi_frame, 1)

roi = cv2.selectROI("Load ROI", select_roi_frame)

Expand All @@ -233,7 +249,7 @@ def plot_text(self, frame, frame_w, fps, plot_xmin, plot_ymin, padding, counter_
aux_xmin = plot_xmin

# FPS counter
label = f"FPS (YOLO + SORT): {fps:.3f}"
label = f"FPS (YOLOv7x + SORT): {fps:.3f}"
# min required space for the text
(w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)

Expand Down Expand Up @@ -272,6 +288,14 @@ def plot_text(self, frame, frame_w, fps, plot_xmin, plot_ymin, padding, counter_

return frame


def change_res(self, cap, max_width, orig_ratio):

cap.set(3,max_width)
cap.set(4, int(max_width * orig_ratio))

return cap

def run(self):

# Debug
Expand Down Expand Up @@ -391,7 +415,7 @@ def run(self):
self.stopped = True
break
else:
if cv2.waitKey(1) & 0xFF == ord('q'):
if cv2.waitKey(int(1000/self.max_fps)) & 0xFF == ord('q'):
logging.info('Exiting by keyboard...')
self.stopped = True
break
Expand All @@ -416,9 +440,7 @@ def run(self):
break

if self.save_vid:
logging.info(f'Writting the results in {self.save_loc}...')
result.write(self.out_frame)
logging.info('The result video has been written.')

# Stopped
logging.info(f'Stopped manually: {self.stopped}')
Expand Down

0 comments on commit f4b2432

Please sign in to comment.