OpenCV.putext:仅大小-1数组问题

时间:2019-02-04 15:43:59

标签: python opencv computer-vision data-science

在下面的脚本中,我正在试验OpenCV,并使用Haar Cascades计算到人脸检测到笔记本电脑网络摄像头的距离。我正在使用带有网络摄像头,Python 3.6和OpenCV 3.4的Windows 10笔记本电脑。

OpenCV.putext出现问题,无法在视频流的视图上显示此计算值…

        text = "Inches{}".format(np.int(inches))
        cv2.putText(gray, text, (roi[0] - 10, roi[1] - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)

如果将其注释掉,代码将运行。任何有关我做错事情的提示都将不胜感激!

import numpy as np
import imutils
import cv2
from imutils.video import VideoStream
from imutils.video import FPS
import time



def distance_to_camera(knownWidth, focalLength, perWidth):
    # compute and return the distance from the maker to the camera
    return (knownWidth * focalLength) / perWidth

face_cascade = cv2.CascadeClassifier('C:/Users/Haar/frontalFace10/haarcascade_frontalface_alt.xml')

#Calculated from a different script
focalLength = 709.0909090909091

#average human head width
knownWidth = 7


# Initialize mutithreading the video stream.
camera = VideoStream(src=0).start()

# Allow the camera to warm up.
time.sleep(2.0)

#start FPS
fps = FPS().start()

roi = None

while True:
        image = camera.read()
        image = imutils.resize(image, width=500)

        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5);
        for (x, y, w, h) in faces:
                cv2.rectangle(gray,(x,y),(x+w,y+h),(255,255,255),2)
                roi = gray[y:y+h, x:x+w]

        if roi is None:
                pass
        else:
                inches = distance_to_camera(knownWidth, focalLength, roi.shape[1])
                print(inches)
                text = "Inches{:.2f}".format(np.int(inches))
                cv2.putText(gray, text, (roi[0] - 10, roi[1] - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)


        cv2.imshow("gray", gray)
        key = cv2.waitKey(1) & 0xFF

        fps.update()

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
                break

fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

camera.stop()
cv2.destroyAllWindows()

这是错误的完整回溯:

Traceback (most recent call last):
  File "C:\Users\distance-to-camera\selectHaar3.py", line 53, in <module>
    cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
TypeError: only size-1 arrays can be converted to Python scalars
>>> 

1 个答案:

答案 0 :(得分:0)

package.json{ "dependencies": { ... "react-crud-shared": "file:./react-crud-shared" } } 是数组。 我认为您正在尝试的方法是:

{
  "functions": {
    "ignore": []
  }
}