关于OpenFace的各种依赖库dependencies 的安装配置部分,可以参见之前的文章dlib, OpenFace and face_recognition。
下面纪录一下测试OpenFace本身的各种应用时遇到的问题。
Demo 4: Real-Time Face Embedding Visualization
Running Command
python demos/sphere.py --networkModel nn4.small2.3d.v1.t7
1) NameError: name ‘xrange’ is not defined
File "demos/sphere.py", line 244/259, in <module>
for i in xrange(len(trackers) - 1, -1, -1):
NameError: name 'xrange' is not defined
NameError: global name ‘xrange’ is not defined in Python 3
xrange() was renamed to range() in Python 3.
2)AttributeError: module ‘cv2’ has no attribute ‘cv’
File "demos/sphere.py", line 279, in <module>
(0, 0, 0), 1, cv2.cv.CV_AA)
AttributeError: module 'cv2' has no attribute 'cv'
Cleaned up camera.
What is the cv2.cv replacement in OpenCV3?
From OpenCV 2.X OpenCV 3.0 a few things changed.
Specifically:
- cv2.cv doesn’t exists in OpenCV 3.0. Use simply cv2.
- some defines changed, e.g. CV_BGR2HSV is now COLOR_BGR2HSV.
Videofacerec Error #38
FROM cv2.cv.CV_AA
or cv2.CV_AA
TO cv2.LINE_AA
3) TypeError: a bytes-like object is required, not ‘str’
File "demos/sphere.py", line 231, in <module>
rep = net.forward(alignedFace)
File "~/anaconda/lib/python3.5/site-packages/openface/torch_neural_net.py", line 205, in forward
rep = self.forwardPath(t)
File "~/anaconda/lib/python3.5/site-packages/openface/torch_neural_net.py", line 164, in forwardPath
self.p.stdin.write(imgPath + "\n")
TypeError: a bytes-like object is required, not 'str'
Python 3 TypeError: must be str, not bytes with sys.stdout.write()
Python 3 handles strings a bit different. Originally there was just one type for strings: str. When unicode gained traction in the ’90s the new unicode type was added to handle Unicode without breaking pre-existing code1. This is effectively the same as str but with multibyte support.
In Python 3 there are two different types:
- The bytes type. This is just a sequence of bytes, Python doesn’t know anything about how to interpret this as characters.
- The str type. This is also a sequence of bytes, but Python knows how to interpret those bytes as characters.
- The separate
unicode
type was dropped. str now supports unicode.This change is incompatible with Python 2 as many return values have changed, leading to subtle problems like this one; it’s probably the main reason why Python 3 adoption has been so slow. Since Python doesn’t have static typing[2] it’s impossible to change this automatically with a script (such as the bundled 2to3).
- You can convert
str
tobytes
with bytes(‘h€llo’, ‘utf-8’); this should produce b’H\xe2\x82\xacllo’. Note how one character was converted to three bytes.- You can convert bytes to str with b’H\xe2\x82\xacllo’.decode(‘utf-8’)
In your specific piece of code,
nextline
is of typebytes
, not str, readingstdout
andstdin
fromsubprocess
changed in Python 3 fromstr
tobytes
. This is because Python can’t be sure which encoding this uses. It probably uses the same as sys.stdin.encoding (the encoding of your system), but it can’t be sure.
4) Final fix
"~/anaconda/lib/python3.5/site-packages/openface/torch_neural_net.py",
line 206,
rep = self.forwardPath(bytes(str(t+"\n"), sys.stdin.encoding))
line 164,
output1 = self.p.stdout.readline()
output = output1.decode(sys.stdout.encoding)
5) NOTE
line 231, rep = net.forward(alignedFace)
net = openface.TorchNeuralNet(args.networkModel,
imgDim=args.imgDim, cuda=args.cuda)
[torch_neural_net.py]
alignedFace = align.align(96, frameSmall, bb,landmarkIndices=openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP)
return: The aligned RGB image. Shape: (imgDim, imgDim, 3)
align = openface.AlignDlib(args.dlibFacePredictor)[align_dlib.py]
Demo 1: Real-Time Web Demo (1)
Running Command
./dem