YACWC
This commit is contained in:
150
hummingbird_choice/create_video.py
Normal file
150
hummingbird_choice/create_video.py
Normal file
@@ -0,0 +1,150 @@
|
||||
import os
|
||||
|
||||
dir_scan = '/srv/ftp/hummingbird/2025/07/14'
|
||||
species_match = {'Archilochus colubris'}
|
||||
import json
|
||||
|
||||
|
||||
json_to_load = list()
|
||||
for cwd, dirs, files in os.walk(dir_scan):
|
||||
for x in files:
|
||||
if x.endswith('.json'):
|
||||
cpath = os.path.join(cwd, x)
|
||||
with open(cpath,'r') as ff:
|
||||
json_to_load.append(json.load(ff))
|
||||
pass
|
||||
# %%
|
||||
|
||||
|
||||
|
||||
|
||||
fr_detections = dict()
|
||||
for js in json_to_load:
|
||||
js_path = js['json']['path']
|
||||
fr_detections[js_path] = list()
|
||||
w = js['video']['w']
|
||||
h = js['video']['h']
|
||||
for frame in js['scores']:
|
||||
fr_num = frame['frame']
|
||||
for det in frame['detections']:
|
||||
if det['name'] in species_match:
|
||||
det['fr_num'] = fr_num
|
||||
fr_detections[js_path].append(det)
|
||||
|
||||
|
||||
to_delete = list()
|
||||
for key, vals in fr_detections.items():
|
||||
if len(vals) == 0:
|
||||
to_delete.append(key)
|
||||
|
||||
|
||||
for x in to_delete:
|
||||
fr_detections.pop(x)
|
||||
# %%
|
||||
import cv2
|
||||
c_key = c_key.rsplit('/',1)[0] + '/hummingbird_00_20250714103229.json'
|
||||
c_vid = c_key.replace('.json','.mp4')
|
||||
cap = cv2.VideoCapture(c_vid)
|
||||
# %%
|
||||
rt_dir = '/home/thebears/Source/video_montages/hummingbird_choice/'
|
||||
files = os.listdir(rt_dir)
|
||||
|
||||
|
||||
cnnum = [x.replace('output_','').replace('.mp4','') for x in files if x.endswith('.mp4')]
|
||||
|
||||
nums = list()
|
||||
for x in cnnum:
|
||||
try:
|
||||
nums.append(int(x))
|
||||
except:
|
||||
pass
|
||||
|
||||
if len(nums) == 0:
|
||||
s_num = 1
|
||||
else:
|
||||
s_num = max(nums)+1
|
||||
|
||||
f_name = 'output_{0:03g}.mp4'.format(s_num)
|
||||
|
||||
c_sec = [0, 0]
|
||||
output_vid_path = os.path.join('/home/thebears/Source/video_montages/hummingbird_choice/',f_name)
|
||||
video_out = cv2.VideoWriter(output_vid_path, cv2.VideoWriter_fourcc(*'mpeg'),30,[1280,720])
|
||||
|
||||
|
||||
import cv2
|
||||
|
||||
c_len = 0
|
||||
total_len = len(list(fr_detections))
|
||||
for c_key in sorted(list(fr_detections)):
|
||||
|
||||
c_len +=1
|
||||
print(c_len, total_len)
|
||||
c_kk = fr_detections[c_key]
|
||||
c_vid = c_key.replace('.json','.mp4')
|
||||
cap = cv2.VideoCapture(c_vid)
|
||||
|
||||
|
||||
fr_nums = [c['fr_num'] for c in c_kk]
|
||||
min_fr = min(fr_nums)
|
||||
max_fr = max(fr_nums)
|
||||
for x in range(min_fr):
|
||||
cap.read();
|
||||
|
||||
coords_vec = [x['coords'] for x in c_kk]
|
||||
cvec = {x['fr_num']:x['coords'] for x in c_kk}
|
||||
|
||||
c_frame = min(fr_nums)
|
||||
color = (255, 0, 0)
|
||||
thickness = 2
|
||||
for x in range(min(fr_nums) ,max(fr_nums)):
|
||||
|
||||
is_ok, fr_data = cap.read();
|
||||
if is_ok:
|
||||
resz = cv2.resize(fr_data, (1280,720))
|
||||
|
||||
if x in cvec:
|
||||
cp = cvec[x]
|
||||
start_point = (int(cp[0]),int(cp[1]-24))
|
||||
end_point = (int(0*cp[0]+cp[2]), int(0*cp[1]+cp[3]-24))
|
||||
if start_point[0] < (1280/2):
|
||||
c_sec[0]+=1
|
||||
else:
|
||||
c_sec[1]+=1
|
||||
|
||||
resz = cv2.rectangle(resz, start_point, end_point, color, thickness)
|
||||
szz_left = '{0:2.2f}s'.format(c_sec[0]/30)
|
||||
szz_right = '{0:2.2f}s'.format(c_sec[1]/30)
|
||||
font = cv2.FONT_HERSHEY_SIMPLEX
|
||||
fontScale = 4
|
||||
thickness = 2
|
||||
cv2.putText(resz, szz_left, (0, 100), font, fontScale,(0,255,0),thickness,cv2.LINE_AA)
|
||||
|
||||
|
||||
text_width, text_height = cv2.getTextSize(szz_right, font, fontScale, thickness)[0]
|
||||
cv2.putText(resz, szz_right, (1280-text_width, 100), font, fontScale,(0,0,255),thickness,cv2.LINE_AA)
|
||||
|
||||
|
||||
#), 1, (255, 0, 0), 2.0, cv2.LINE_AA)
|
||||
video_out.write(resz)
|
||||
|
||||
cap.release()
|
||||
|
||||
video_out.release()
|
||||
# %%
|
||||
|
||||
|
||||
import cv2
|
||||
c_kk = fr_detections[c_key]
|
||||
fr_nums = [c['fr_num'] for c in c_kk]
|
||||
min_fr = min(fr_nums)
|
||||
max_fr = max(fr_nums)
|
||||
|
||||
|
||||
coords_vec = [x['coords'] for x in c_kk]
|
||||
|
||||
if False:
|
||||
import matplotlib.pyplot as plt
|
||||
plt.ion()
|
||||
plt.plot(fr_nums, coords_vec)
|
||||
plt.legend(['x','y','w','h'])
|
||||
# %%
|
||||
Reference in New Issue
Block a user