-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputLandmarks.py
126 lines (93 loc) · 4.18 KB
/
outputLandmarks.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Output landmark features for the bounding boxes given for each frame
# in a video file
#Import required modules
import cv2
import dlib
import argparse
import pandas as pd
import numpy as np
parser = argparse.ArgumentParser(description="Use dlib's landmark detector to calculate facial features and output their positions")
parser.add_argument("--infile", dest="infile", type=str, required=True)
parser.add_argument("--outfile", dest = "outfile", type = str, required = True)
parser.add_argument("--bboxes", dest = "bboxes", type = str, required = False)
parser.add_argument("--showvideo", dest = "showvideo", action = "store_true")
parser.set_defaults(showvideo = False)
parser.add_argument("--skipframe", dest = "skipframe", type=int, action="store")
parser.add_argument("--skipms", dest = "skipms", type = int, action = "store")
args = parser.parse_args()
trackedpoints = [] # for our output data
if args.showvideo == True:
cv2.namedWindow("landmark")
video_capture = cv2.VideoCapture(args.infile)
if(args.skipframe and args.skipms):
print("Cannot skip frames and time")
quit()
#rect = dlib.rectangle( left = 290, right = 333, top = 127, bottom = 84)
if(args.skipframe):
print("Skipping " + str(args.skipframe) + " frames")
video_capture.set(cv2.CAP_PROP_POS_FRAMES, args.skipframe)
if(args.skipms):
print("Skipping " + str(args.skipms) + " ms")
video_capture.set(cv2.CAP_PROP_POS_MSEC, args.skipms)
if not args.bboxes:
print("Haven't yet implemented face detection")
quit()
bboxes = pd.read_csv(args.bboxes, skiprows = 1, names=["frame",
"time",
"actpt",
"bbcx",
"bbcy",
"bbw",
"bbh",
"bbrot",
"bbv1x",
"bbv1y",
"bbv2x",
"bbv2y",
"bbv3x",
"bbv3y",
"bbv4x",
"bbv4y"])
detector = dlib.get_frontal_face_detector() #Face detector
predictor = dlib.shape_predictor("/usr/share/dlib/shape_predictor_68_face_landmarks.dat")
ret, img = video_capture.read()
while ret:
frame = video_capture.get(cv2.CAP_PROP_POS_FRAMES)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# This improves contrast, but seems to amplify compression noise
#clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
#clahe_image = clahe.apply(gray)
#detections = detector(clahe_image, 1) #Detect the faces in the image
print frame
# Create bounding box rectangle from loaded file
testframe = bboxes[bboxes['frame']== frame]
if testframe.empty:
print("No bbox found for frame" + str(frame))
else:
thisframe = testframe.iloc[0]
if(thisframe['bbrot'] != 0):
print("Bounding box rotation must be 0")
quit()
bbox = dlib.rectangle( left = long(thisframe.bbv1x), \
right = long(thisframe.bbv3x), top = long(thisframe.bbv3y), \
bottom = long(thisframe.bbv1y))
shape = predictor(gray, bbox) #Get coordinates
thesepoints = [frame]
# Extract coordinates of each part
for i in range(0, shape.num_parts):
thesepoints.extend([shape.part(i).x, shape.part(i).y])
trackedpoints.append(thesepoints)
if args.showvideo == True:
for i in range(0,shape.num_parts):
cv2.circle(img, (shape.part(i).x, shape.part(i).y), 1, (0,0,255), thickness=1)
cv2.rectangle(img, (int(bbox.left()), int(bbox.top())), (int(bbox.right()), int(bbox.bottom())), color = (255,255,255), thickness=1)
cv2.imshow("image", img)
if cv2.waitKey(1) & 0xFF == ord('q'): #Exit program when the user presses 'q'
break
ret, img = video_capture.read()
# Define column names and output
colnames = ["frame"]
for i in range(0,shape.num_parts):
colnames.extend(["p"+str(i+1)+"x", "p"+str(i+1)+"y"])
outdata = pd.DataFrame.from_records(trackedpoints, columns=colnames)
outdata.to_csv(args.outfile)