CMU OpenFace

本文记录了在测试OpenFace实时面部嵌入可视化Demo 4时遇到的Python 2到3迁移问题,包括NameError: 'xrange'未定义、cv2模块变化以及类型转换错误等,并提供了相应的修复方法。此外,还概述了运行实时Web Demo (1)和(2)时导入模块如txaio、Twisted、Autobahn等问题及其解决策略。
摘要由CSDN通过智能技术生成

关于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

OpenCV 2–> 3 Changes overview

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 to bytes 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 type bytes, not str, reading stdout and stdin from subprocess changed in Python 3 from str to bytes. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值