forked from ririgoei/CS160-Team3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getpoints
105 lines (80 loc) · 2.96 KB
/
getpoints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python
## NOTE: This is not functional because the database command
## probably needs to be changed? Currently this script only
## prints the info to the screen instead of executing the
## database commands.
# getallpoints :
#
# This takes one argument, the video id number.
# It takes each saved frame image of the video
# and obtains 68 face-points from each, saving
# everything into the database.
# This script should be in Apache cgi-bin.
#
# Videos are expected to be in /cs160/videos/<#>.mp4
#
# Frames (grayscale) are expected to be in:
# /cs160/frames/<videoid>/color/<videoid>.<frame#>.png
#
import sys
import subprocess
import os
import os.path
import string
import MySQLdb
myname = sys.argv[0]
db = MySQLdb.connect("127.0.0.1:3306","root","root","cs160");
cursor = db.cursor()
if ((len(sys.argv) < 2 or (len(sys.argv) > 2))):
print "syntax: " + myname + " <video-id#>"
sys.exit(1)
vidid = sys.argv[1]
videopath = "/cs160/videos/"
videofile = videopath + vidid + ".mp4"
imgdir = "/cs160/frames/" + vidid + "/gray/"
# print "vpth is : " + videopath
# print "vfile is : " + videofile
# print "imgdir is : " + imgdir
if not os.path.isfile(videofile):
print "error: video file " + videofile + " not found."
sys.exit(1)
getcmd = "ffprobe -v 0 -select_streams v:0 -show_entries stream=nb_frames " \
+ videofile + "|grep -v STREAM|cut -f 2 -d="
nb_frames = subprocess.check_output(getcmd, shell=True)
fn = nb_frames.strip()
print "please wait...this may take over " + fn + " minutes..."
for i in xrange(1, int(nb_frames) + 1):
frameid = '{:d}'.format(i)
imgfile = vidid + "." + frameid + ".png"
if not os.path.isfile(imgdir + imgfile):
print "err: frame file " + imgdir + imgfile + " not found."
sys.exit(2)
tfile = "/tmp/list_det_0.txt"
# print "processing " + imgfile
# print "imgid = " + frameid
getpointscmd = "FaceLandmarkImg -f " + imgfile + " -inroot " + imgdir + " -of list.txt -outroot /tmp"
a = subprocess.check_output(getpointscmd, shell=True)
catcmd = "cat " + tfile + "|grep -A 68 npoints" \
+ "|grep -v npoints|grep -v {|grep -v }"
points = subprocess.check_output(catcmd, shell=True)
# print "-------"
# print points
# print "-------"
pointnum = 1
for pline in points.splitlines():
# print "got a line:"
# print pline
# Need to change the database command syntax??
dbline = "postgres add video=" + vidid + " frames=" + str(i)
dbline = dbline + " point=" + str(pointnum)
xy = pline.split()
x = "x_"+pointNum
y = "y_"+pointNum
query = "INSERT INTO cs160.openface_data("+x+","+y+",video_id) VALUES ("+xy[0]+","+xy[1]+","
+vidid+")ON DUPLICATE KEY UPDATE "+x+"="+xy[0]+", " + y + "=" + xy[1] + ";"
cursor.execute(query);
dbline = dbline + " x=" + xy[0] + " y=" + xy[1]
pointnum = pointnum + 1
# Check output?
# a = subprocess.check_output(dbline, shell=True)
print dbline