f
This commit is contained in:
62
flag_videos_to_keep.py
Normal file
62
flag_videos_to_keep.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import json
|
||||||
|
import shutil
|
||||||
|
import os
|
||||||
|
source_path = '/srv/ftp/hummingbird/2021'
|
||||||
|
#target_path = '/home/thebears/Videos/ftp'
|
||||||
|
target_path = '/home/thebears/ftp_links'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
have_json = set()
|
||||||
|
for di, _, fns in os.walk(source_path):
|
||||||
|
for fn in fns:
|
||||||
|
if fn.endswith('.json'):
|
||||||
|
have_json.add(os.path.join(di, fn))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
do_stop = False
|
||||||
|
fracs = dict()
|
||||||
|
|
||||||
|
for c_js in have_json:
|
||||||
|
hits = 0
|
||||||
|
total = 0
|
||||||
|
o = json.load(open(c_js,'r'))
|
||||||
|
for i in o:
|
||||||
|
total += 1
|
||||||
|
# if len([x for x in i['scores'] if x > 0.1]) > 0:
|
||||||
|
if len(i['boxes']) > 0:
|
||||||
|
hits += 1
|
||||||
|
|
||||||
|
fracs[c_js] = [hits, total]
|
||||||
|
|
||||||
|
|
||||||
|
if do_stop:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ratios = dict()
|
||||||
|
for x,y in fracs.items():
|
||||||
|
ratios[x] = y[0]/y[1]
|
||||||
|
# %%
|
||||||
|
import math
|
||||||
|
dir_created = set()
|
||||||
|
for fname, ratio in ratios.items():
|
||||||
|
cr = math.floor(ratio * 10)/10
|
||||||
|
target_dir = os.path.join(target_path, str(cr))
|
||||||
|
if not os.path.exists(target_dir) and target_dir not in dir_created:
|
||||||
|
os.mkdir(target_dir)
|
||||||
|
dir_created.add(target_dir)
|
||||||
|
else:
|
||||||
|
dir_created.add(target_dir)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
source_file = fname.replace('.json','.mp4')
|
||||||
|
target_file = os.path.join(target_dir, os.path.basename(source_file))
|
||||||
|
|
||||||
|
os.symlink(source_file, target_file)
|
||||||
23
models/20210701_202822.json
Normal file
23
models/20210701_202822.json
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"categories": [
|
||||||
|
{
|
||||||
|
"supercategory": "Aves",
|
||||||
|
"id": 206,
|
||||||
|
"name": "Archilochus colubris",
|
||||||
|
"new_id": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"supercategory": "Aves",
|
||||||
|
"id": 4493,
|
||||||
|
"name": "Icterus galbula",
|
||||||
|
"new_id": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"supercategory": "Aves",
|
||||||
|
"id": 403,
|
||||||
|
"name": "Poecile atricapillus",
|
||||||
|
"new_id": 3
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"model_type": "fasterrcnn_mobilenet_v3_large_fpn"
|
||||||
|
}
|
||||||
35
score_in_directory.py
Normal file
35
score_in_directory.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import os
|
||||||
|
import random
|
||||||
|
from multiprocessing import Pool
|
||||||
|
import sys
|
||||||
|
sys.path.append('/home/thebears/Seafile/Designs/ML')
|
||||||
|
from score_video import score_video
|
||||||
|
|
||||||
|
rtpath = '/srv/ftp/hummingbird/2021'
|
||||||
|
cmd = '/usr/bin/python3 /home/thebears/Seafile/Designs/ML/inaturalist_models/score_video.py {mp4name}'
|
||||||
|
have_json = set()
|
||||||
|
fnames = set()
|
||||||
|
for di,_, fns in os.walk(rtpath):
|
||||||
|
for fn in fns:
|
||||||
|
if fn.endswith('.mp4'):
|
||||||
|
fnames.add(os.path.join(di,fn))
|
||||||
|
elif fn.endswith('.json'):
|
||||||
|
have_json.add(os.path.join(di,fn.replace('.json','.mp4')))
|
||||||
|
|
||||||
|
files_to_score = list(fnames - have_json)
|
||||||
|
random.shuffle(files_to_score)
|
||||||
|
|
||||||
|
|
||||||
|
def try_catch_chunk(vids):
|
||||||
|
try:
|
||||||
|
score_video(vids)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
lst = files_to_score
|
||||||
|
n = 25
|
||||||
|
chunks = [lst[i:i + n] for i in range(0, len(lst), n)]
|
||||||
|
# %%
|
||||||
|
if __name__ == '__main__':
|
||||||
|
with Pool(4) as p:
|
||||||
|
output = p.map(score_video,chunks)
|
||||||
129
score_video.py
Normal file
129
score_video.py
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
|
||||||
|
|
||||||
|
import torchvision
|
||||||
|
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
|
||||||
|
from collections import defaultdict as ddict
|
||||||
|
import json
|
||||||
|
import torch
|
||||||
|
from torchvision import datasets, transforms as T
|
||||||
|
import numpy as np
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
sys.path.append('/home/thebears/Seafile/Designs/ML')
|
||||||
|
import json
|
||||||
|
import cv2
|
||||||
|
import random
|
||||||
|
|
||||||
|
from model import Model
|
||||||
|
import socket
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#vid_path = '/home/thebears/data/hummingbird_videos/Hummingbird_01_20210601055009.mp4'
|
||||||
|
|
||||||
|
def score_video(vid_in_list):
|
||||||
|
|
||||||
|
|
||||||
|
no_cuda = socket.gethostname() == 'tree'
|
||||||
|
device='cpu'
|
||||||
|
model_rt_path = '/home/thebears/Seafile/Designs/ML/inaturalist_models/models/'#0210701_202822.json
|
||||||
|
newest_model = os.path.join(model_rt_path, max(os.listdir(model_rt_path)).replace('.pth',''))
|
||||||
|
with open(newest_model + '.json','r') as nmj:
|
||||||
|
model_json = json.load(nmj)
|
||||||
|
|
||||||
|
cats = model_json['categories']
|
||||||
|
cats.sort(key=lambda x: x['new_id'])
|
||||||
|
num_cat = len(cats) + 1
|
||||||
|
model_type = model_json['model_type']
|
||||||
|
model = Model(num_cat, model_type)
|
||||||
|
labels = [x['name'] for x in cats]
|
||||||
|
model.load_state_dict(
|
||||||
|
torch.load(newest_model + '.pth', map_location = torch.device(device))
|
||||||
|
)
|
||||||
|
model.eval()
|
||||||
|
|
||||||
|
if isinstance(vid_in_list, str):
|
||||||
|
vid_in_list = [vid_in_list]
|
||||||
|
|
||||||
|
for idx_vid, vid_in in enumerate(vid_in_list):
|
||||||
|
vid_path = os.path.abspath(vid_in)
|
||||||
|
scores_json = vid_path.rsplit('.')[0]+'.json'
|
||||||
|
print(os.getpid(),':',str(idx_vid),'/',str(len(vid_in_list)),vid_path)
|
||||||
|
if os.path.exists(scores_json):
|
||||||
|
print(f"JSON {scores_json} already exists")
|
||||||
|
exit()
|
||||||
|
vid_dir = os.path.dirname(vid_path)
|
||||||
|
os.system(f'sudo chmod 777 {vid_dir}')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
cap = cv2.VideoCapture(vid_path)
|
||||||
|
|
||||||
|
from torchvision.utils import draw_bounding_boxes
|
||||||
|
import torch as t
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import matplotlib
|
||||||
|
cap = cv2.VideoCapture(vid_path)
|
||||||
|
frame_num = 0
|
||||||
|
|
||||||
|
results = list()
|
||||||
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
||||||
|
|
||||||
|
|
||||||
|
for frame_num in range(0, total_frames, 30):
|
||||||
|
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
|
||||||
|
img = cap.read()[1]
|
||||||
|
image = img[:, :, ::-1].copy()
|
||||||
|
o = T.ToTensor()(image)
|
||||||
|
img = o[None, :, :, :]
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
ou = model(img)
|
||||||
|
|
||||||
|
if len(ou) > 0:
|
||||||
|
ofscore = ou[0]
|
||||||
|
|
||||||
|
for k in ofscore:
|
||||||
|
ofscore[k] = ofscore[k].numpy().tolist()
|
||||||
|
|
||||||
|
ofscore['names'] = [labels[x-1] for x in ofscore['labels']]
|
||||||
|
ofscore['frame_number'] = frame_num
|
||||||
|
|
||||||
|
results.append(ofscore)
|
||||||
|
|
||||||
|
with open(scores_json,'w') as jj:
|
||||||
|
json.dump(results, jj, indent=4)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
score_video(sys.argv[1])
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
# vid_path = '/srv/ftp/hummingbird/2021/06/27/Hummingbird_01_20210627101803.mp4'
|
||||||
|
# import time
|
||||||
|
# import cv2
|
||||||
|
# video = cv2.VideoCapture(vid_path)
|
||||||
|
|
||||||
|
# total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
||||||
|
# # %%
|
||||||
|
# st = time.time()
|
||||||
|
|
||||||
|
# while True:
|
||||||
|
# ret, read = video.read()
|
||||||
|
# if not ret:
|
||||||
|
# break
|
||||||
|
|
||||||
|
# et = time.time()
|
||||||
|
|
||||||
|
# print(et-st)
|
||||||
|
|
||||||
|
# st = time.time()
|
||||||
|
# frs = list()
|
||||||
|
# for i in range(0,total_frames, 150):
|
||||||
|
# video.set(cv2.CAP_PROP_POS_FRAMES, i)
|
||||||
|
# ret, frame = video.read()
|
||||||
|
# frs.append(frame)
|
||||||
|
# et = time.time()
|
||||||
|
# print(et-st)
|
||||||
123
test.py
Normal file
123
test.py
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
# %%
|
||||||
|
import torchvision
|
||||||
|
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
|
||||||
|
from collections import defaultdict as ddict
|
||||||
|
import json
|
||||||
|
import torch
|
||||||
|
from torchvision import datasets, transforms as T
|
||||||
|
import numpy as np
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import cv2
|
||||||
|
import random
|
||||||
|
from model import Model
|
||||||
|
from torchvision.utils import draw_bounding_boxes
|
||||||
|
import torch as t
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
device = 'cpu'
|
||||||
|
|
||||||
|
model_rt_path = '/home/thebears/Seafile/Designs/ML/inaturalist_models/models/'#0210701_202822.json
|
||||||
|
newest_model = os.path.join(model_rt_path, max(os.listdir(model_rt_path)).replace('.pth',''))
|
||||||
|
with open(newest_model + '.json','r') as nmj:
|
||||||
|
model_json = json.load(nmj)
|
||||||
|
|
||||||
|
cats = model_json['categories']
|
||||||
|
cats.sort(key=lambda x: x['new_id'])
|
||||||
|
num_cat = len(cats) + 1
|
||||||
|
model_type = model_json['model_type']
|
||||||
|
model = Model(num_cat, model_type)
|
||||||
|
labels = [x['name'] for x in cats]
|
||||||
|
model.load_state_dict(
|
||||||
|
torch.load(newest_model + '.pth', map_location=torch.device(device))
|
||||||
|
)
|
||||||
|
model.eval()
|
||||||
|
model.to(device)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#rtdir = '/home/thebears/data/hummingbird_imagenet/hummingbird'
|
||||||
|
|
||||||
|
#ff = [x for x in os.listdir(rtdir) if x.endswith('.jpg')]
|
||||||
|
|
||||||
|
# img = os.path.join(rtdir, random.choice(ff))
|
||||||
|
# image = cv2.imread(img)[:, :, ::-1].copy()
|
||||||
|
# o = T.ToTensor()(image).to(device)
|
||||||
|
# img = o[None, :, :, :]
|
||||||
|
|
||||||
|
# ou = model(img)
|
||||||
|
|
||||||
|
#oimage = t.tensor(image, dtype=t.uint8).permute([2, 0, 1])
|
||||||
|
|
||||||
|
#matplotlib.use('Qt5Agg')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#vid_path = '/home/thebears/data/hummingbird_videos/Hummingbird_01_20210601055009.mp4'
|
||||||
|
#vid_path = '/home/thebears/data/hummingbird_videos/Hummingbird_01_20210617113038.mp4'
|
||||||
|
|
||||||
|
print('model loaded')
|
||||||
|
# %%
|
||||||
|
vid_path = '/home/thebears/data/hummingbird_videos/Hummingbird_01_20210617113038.mp4'
|
||||||
|
cap = cv2.VideoCapture(vid_path)
|
||||||
|
imgs = list()
|
||||||
|
|
||||||
|
#movie = cv2.VideoWriter('/home/thebears/Seafile/Designs/ML/inaturalist_models/output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (2560,1920))
|
||||||
|
|
||||||
|
frame_num = 0
|
||||||
|
# %%
|
||||||
|
while cap.isOpened():
|
||||||
|
ret, frame = cap.read()
|
||||||
|
if not ret:
|
||||||
|
break
|
||||||
|
|
||||||
|
# if frame_num % 10 == 1:
|
||||||
|
img = cap.read()[1]
|
||||||
|
image = img[:, :, ::-1].copy()
|
||||||
|
o = T.ToTensor()(image).to(device)
|
||||||
|
img = o[None, :, :, :]
|
||||||
|
|
||||||
|
ou = model(img)
|
||||||
|
|
||||||
|
idx = ou[0]['labels']
|
||||||
|
label_names = [labels[x-1] for x in idx]
|
||||||
|
|
||||||
|
|
||||||
|
scores = ou[0]['scores']
|
||||||
|
oimage = t.tensor(255*img.squeeze(), dtype=t.uint8)
|
||||||
|
boxes = ou[0]['boxes']
|
||||||
|
|
||||||
|
if boxes.shape[0] > 1:
|
||||||
|
boxes = boxes[[1],:]
|
||||||
|
label_names = [label_names[0]]
|
||||||
|
|
||||||
|
if boxes.shape[0] > 0:
|
||||||
|
label_names[0] += ' {0:0.2f}'.format(scores[0])
|
||||||
|
|
||||||
|
|
||||||
|
ox = draw_bounding_boxes(oimage, boxes, width=5, labels = label_names,
|
||||||
|
font='Victor Mono SemiBold Nerd Font Complete Mono Windows Compatible',font_size=50, fill = False, colors = (255, 255, 100, 100))
|
||||||
|
fname = '/home/thebears/Seafile/Designs/ML/inaturalist_models/frames/frame_{0:06g}.jpg'.format(frame_num)
|
||||||
|
from PIL import Image
|
||||||
|
im = Image.fromarray(np.uint8(ox.permute([1,2,0]).numpy()))
|
||||||
|
im.save(fname)
|
||||||
|
|
||||||
|
# plt.imshow(ox.permute([1, 2, 0]))
|
||||||
|
frame_num += 1
|
||||||
|
print(frame_num)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user