鱼C论坛

 找回密码
 立即注册
查看: 2097|回复: 1

用python给微信头像加圣诞帽的疑问

[复制链接]
发表于 2017-12-28 10:01:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zhuangrui_tea 于 2017-12-28 10:02 编辑

这是作者在github的源代码(github源码)
shape_predictor_5_face_landmarks.dat
我的运行环境:OSX10.12.3+python3.5+pycharm
运行代码后提示错误:

Connected to pydev debugger (build 172.4343.24)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/zhuangrui/Documents/Python/Python_practice/WeChat_Christmas_Cap.py", line 123, in <module>
    output = add_hat(img, hat_img)
  File "/Users/zhuangrui/Documents/Python/Python_practice/WeChat_Christmas_Cap.py", line 34, in add_hat
    shape = predictor(img, d)
TypeError: 'NoneType' object is not callable

错误是:'NoneType' object is not callable,nonetype不可调用,提示line 34 : shape = predictor(img, d),其中 predictor() 为predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat"),print的结果为none

这个问题如何解决呢?谢谢!

  1. import dlib
  2. import numpy
  3. from skimage import io
  4. import cv2


  5. # 给img中的人头像加上圣诞帽,人脸最好为正脸
  6. def add_hat(img, hat_img):
  7.     # 分离rgba通道,合成rgb三通道帽子图,a通道后面做mask用
  8.     r, g, b, a = cv2.split(hat_img)
  9.     rgb_hat = cv2.merge((r, g, b))

  10.     cv2.imwrite("hat_alpha.jpg", a)
  11.     # ------------------------- 用dlib的人脸检测代替OpenCV的人脸检测-----------------------

  12.     # dlib人脸关键点检测器
  13.     #predictor_path = "shape_predictor_68_face_landmarks.dat"
  14.     #predictor = dlib.shape_predictor(predictor_path)
  15.     predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
  16.     # dlib正脸检测器
  17.     detector = dlib.get_frontal_face_detector()

  18.     # 正脸检测
  19.     dets = detector(img, 1)

  20.     # 如果检测到人脸
  21.     if len(dets) > 0:
  22.         for d in dets:
  23.             x, y, w, h = d.left(), d.top(), d.right() - d.left(), d.bottom() - d.top()
  24.             # x,y,w,h = faceRect
  25.             # cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2,8,0)

  26.             # 关键点检测,5个关键点
  27.             shape = predictor(img, d)
  28.             # for point in shape.parts():
  29.             #     cv2.circle(img,(point.x,point.y),3,color=(0,255,0))

  30.             # cv2.imshow("image",img)
  31.             # cv2.waitKey()

  32.             # 选取左右眼眼角的点
  33.             point1 = shape.part(0)
  34.             point2 = shape.part(2)

  35.             # 求两点中心
  36.             eyes_center = ((point1.x + point2.x) // 2, (point1.y + point2.y) // 2)

  37.             # cv2.circle(img,eyes_center,3,color=(0,255,0))
  38.             # cv2.imshow("image",img)
  39.             # cv2.waitKey()

  40.             #  根据人脸大小调整帽子大小
  41.             factor = 1.5
  42.             resized_hat_h = int(round(rgb_hat.shape[0] * w / rgb_hat.shape[1] * factor))
  43.             resized_hat_w = int(round(rgb_hat.shape[1] * w / rgb_hat.shape[1] * factor))

  44.             if resized_hat_h > y:
  45.                 resized_hat_h = y - 1

  46.             # 根据人脸大小调整帽子大小
  47.             resized_hat = cv2.resize(rgb_hat, (resized_hat_w, resized_hat_h))

  48.             # 用alpha通道作为mask
  49.             mask = cv2.resize(a, (resized_hat_w, resized_hat_h))
  50.             mask_inv = cv2.bitwise_not(mask)

  51.             # 帽子相对与人脸框上线的偏移量
  52.             dh = 0
  53.             dw = 0
  54.             # 原图ROI
  55.             # bg_roi = img[y+dh-resized_hat_h:y+dh, x+dw:x+dw+resized_hat_w]
  56.             bg_roi = img[y + dh - resized_hat_h:y + dh,
  57.                      (eyes_center[0] - resized_hat_w // 3):(eyes_center[0] + resized_hat_w // 3 * 2)]

  58.             # 原图ROI中提取放帽子的区域
  59.             bg_roi = bg_roi.astype(float)
  60.             mask_inv = cv2.merge((mask_inv, mask_inv, mask_inv))
  61.             alpha = mask_inv.astype(float) / 255

  62.             # 相乘之前保证两者大小一致(可能会由于四舍五入原因不一致)
  63.             alpha = cv2.resize(alpha, (bg_roi.shape[1], bg_roi.shape[0]))
  64.             # print("alpha size: ",alpha.shape)
  65.             # print("bg_roi size: ",bg_roi.shape)
  66.             bg = cv2.multiply(alpha, bg_roi)
  67.             bg = bg.astype('uint8')

  68.             cv2.imwrite("bg.jpg", bg)
  69.             # cv2.imshow("image",img)
  70.             # cv2.waitKey()

  71.             # 提取帽子区域
  72.             hat = cv2.bitwise_and(resized_hat, resized_hat, mask=mask)
  73.             cv2.imwrite("hat.jpg", hat)

  74.             # cv2.imshow("hat",hat)
  75.             # cv2.imshow("bg",bg)

  76.             # print("bg size: ",bg.shape)
  77.             # print("hat size: ",hat.shape)

  78.             # 相加之前保证两者大小一致(可能会由于四舍五入原因不一致)
  79.             hat = cv2.resize(hat, (bg_roi.shape[1], bg_roi.shape[0]))
  80.             # 两个ROI区域相加
  81.             add_hat = cv2.add(bg, hat)
  82.             # cv2.imshow("add_hat",add_hat)

  83.             # 把添加好帽子的区域放回原图
  84.             img[y + dh - resized_hat_h:y + dh,
  85.             (eyes_center[0] - resized_hat_w // 3):(eyes_center[0] + resized_hat_w // 3 * 2)] = add_hat

  86.             # 展示效果
  87.             # cv2.imshow("img",img )
  88.             # cv2.waitKey(0)

  89.             return img


  90. # 读取帽子图,第二个参数-1表示读取为rgba通道,否则为rgb通道
  91. hat_img = cv2.imread("hat2.png", -1)

  92. # 读取头像图
  93. img = cv2.imread("test.jpg")
  94. output = add_hat(img, hat_img)

  95. # 展示效果
  96. cv2.imshow("output", output)
  97. cv2.waitKey(0)
  98. cv2.imwrite("output.jpg", output)
  99. # import glob as gb

  100. # img_path = gb.glob("./images/*.jpg")

  101. # for path in img_path:
  102. #     img = cv2.imread(path)

  103. #     # 添加帽子
  104. #     output = add_hat(img,hat_img)

  105. #     # 展示效果
  106. #     cv2.imshow("output",output )
  107. #     cv2.waitKey(0)

  108. cv2.destroyAllWindows()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-12-28 10:04:29 | 显示全部楼层
不知为何发帖超链接不显示,直接上网址:
github的源码:https://github.com/LiuXiaolong19920720/Add-Christmas-Hat
shape_predictor_5_face_landmarks:https://github.com/LiuXiaolong19920720/Add-Christmas-Hat/blob/master/shape_predictor_5_face_landmarks.dat
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-26 19:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表