Last active
January 4, 2019 13:09
-
-
Save yiminglin-ai/294361517afe4b422e70b3b9823d10b9 to your computer and use it in GitHub Desktop.
[mobiface] #python #tracking #mobiface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import call | |
from glob import glob | |
import threading | |
import queue | |
anno_files = glob('./*.csv') | |
vids = [] | |
for af in anno_files: | |
with open(af) as f: | |
lines = f.readlines() | |
lines = lines[1:] | |
ids = [l.split(',')[0] for l in lines] | |
vids = vids + ids | |
vids = set(vids) | |
q = queue.Queue() | |
def worker(): | |
vid = q.get() | |
if vid is None: | |
return | |
command = "youtube-dl https://www.youtube.com/watch?v={} --id".format(vid) | |
call(command.split(), shell=False) | |
for vid in vids: | |
q.put(vid) | |
threads = [ threading.Thread(target=worker) for _i in range(20) ] | |
for thread in threads: | |
thread.start() | |
q.put(None) # one EOF marker for each thread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import time | |
from eco import ECOTracker | |
import cv2 | |
import glob | |
import os | |
from tqdm import tqdm | |
from PIL import Image | |
import json | |
def run_ECO(seq, rp, saveimage): | |
x = seq.init_rect[0] | |
y = seq.init_rect[1] | |
w = seq.init_rect[2] | |
h = seq.init_rect[3] | |
frames = [np.array(Image.open(filename)) for filename in seq.s_frames] | |
# frames = [cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB) for filename in seq.s_frames] | |
if len(frames[0].shape) == 3: | |
is_color = True | |
else: | |
is_color = False | |
frames = [frame[:, :, np.newaxis] for frame in frames] | |
tic = time.clock() | |
# starting tracking | |
tracker = ECOTracker(is_color) | |
res = [] | |
for idx, frame in enumerate(frames): | |
print(idx) | |
if idx == 0: | |
bbox = (x, y, w, h) | |
tracker.init(frame, bbox) | |
bbox = (bbox[0], bbox[1], bbox[0]+bbox[2], bbox[1]+bbox[3]) | |
elif idx < len(frames) - 1: | |
bbox = tracker.update(frame, True) | |
else: # last frame | |
bbox = tracker.update(frame, False) | |
res.append((bbox[0], bbox[1], bbox[2]-bbox[0], bbox[3]-bbox[1])) | |
duration = time.clock() - tic | |
result = {} | |
result['res'] = res | |
result['type'] = 'rect' | |
result['fps'] = round(seq.len / duration, 3) | |
return result | |
class Sequence: | |
def __init__(self,name): | |
self.name = name | |
if __name__ == "__main__": | |
dataset_folder = '/home/yiming/projects/mobiface_new/old/' | |
with open(dataset_folder + 'SEQUENCES') as f: | |
seqs = [Sequence(l.strip()) for l in f] | |
for seq in seqs: | |
seq.folder = os.path.join(dataset_folder, seq.name) | |
seq.s_frames = sorted(glob.glob( seq.folder + '/img/*.png')) | |
gt = np.loadtxt(seq.folder + '/groundtruth_rect.txt', delimiter=',') | |
seq.len = len(gt) | |
seq.init_rect = gt[0] | |
results = run_ECO(seq, 0,0) | |
savename = './results/' + seq.name + '_ECO.json' | |
print('dumping results to ', savename) | |
with open(savename, 'w') as f: | |
json.dump(results, f, sort_keys=True, indent=2) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_sequence(seq, seq_home): | |
img_dir = seq_home / seq / 'img' | |
gt_path = seq_home / seq / 'groundtruth_rect.txt' | |
img_list = list(img_dir.glob('*.jpg')) | |
img_list.sort() | |
with gt_path.open() as f: | |
gt = np.loadtxt((x.replace(',',' ') for x in f)) | |
return img_list, gt[0], gt | |
# save to csv | |
import csv | |
with open('example.csv', 'w') as myFile: | |
myFields = ['rect', 'speed'] | |
writer = csv.DictWriter(myFile, fieldnames=myFields) | |
writer.writeheader() | |
writer.writerow({'rect' : 'France', 'speed': 'Paris'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment