【问题标题】:How to print x and y coordinates in opencv using python?如何使用python在opencv中打印x和y坐标?
【发布时间】:2021-03-25 20:46:34
【问题描述】:

这是我下面的代码,它会输出人脸被识别但现在我想打印出人被识别的 x 和 y 坐标。我会在 for 循环中还是在循环之外包含 x 和 y 坐标的打印语句?请帮忙!谢谢!

import numpy as np
import cv2
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
prevTime = 0
## This will get our web camera
 
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
    retval, frame = cap.read()
    if not retval:
        break
    _, img = cap.read()              ## This gets each frame from the video, cap.read returns 2 variables flag - indicate frame is correct and 2nd is f
    ##img = cv2.imread('Z.png') Then we get our image we want to use
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)   # This method only works on gray skin images, so we have to convert the gray scale to rgb image
    faces = face_cascade.detectMultiScale(gray, 1.1, 5) ## Next, we detect the faces
    if len(faces) > 0:
        print("[INFO] found {0} faces!".format(len(faces)))
        GPIO.output(18,GPIO.HIGH)
    else:
        print("No face")
        GPIO.output(18,GPIO.LOW)
    for (x, y, w, h) in faces:   ## We draw a rectangle around the faces so we can see it correctly
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0))         ## The faces will be a list of coordinates
        cv2.putText(img, 'Myface', (x, y), font, fontScale=1, color=(255,70,120),thickness=2)
    cv2.putText(frame, 'Number of Faces Detected: ' + str, (0,  100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
    cv2.imshow('img', img) ## Last we show the image
    x = cv2.waitKey(30) & 0xff
    if x==27:
        break
## Press escape to exit the program
cap.release()

【问题讨论】:

  • 您可以更简单地格式化您的代码,方法是用鼠标选择它并单击格式工具栏中 BoldItalic 旁边的 {} - 或在每行的开头放置 4 个空格。
  • 我冒昧地为他这样做。缩进应该不受影响,但没有明显的“段落”结构。

标签: python opencv face-detection face-recognition raspberry-pi4


【解决方案1】:

您应该在里面打印,打印 4 个检测为:

print('Top left corner: 'str(x)+' '+str(y))
print('Top right corner: 'str(x+w)+' '+str(y))
etc

【讨论】:

  • 第二个应该是Top right corner,对吧?
  • 好例子!很有帮助
【解决方案2】:

把它放在循环里面。 试试这个:

faces = face_cascade.detectMultiScale(gray, 1.1, 5) 
for (x, y, w, h) in faces:
    x1 = x
    x2 = x + w
    y1 = y
    y2 = y + h
    print ("diaginal point 1 (x1,y1) = ({},{})".format(x1, y1))
    print ("diaginal point 2 (x2,y2) = ({},{})".format(x2, y2))

但是如果你不希望它一直打印它并且如果你按下“P”或“p”这样的特定键就打印一次,那么使用这个代码:

faces = face_cascade.detectMultiScale(gray, 1.1, 5) 
for (x, y, w, h) in faces:
    
    x1 = x
    x2 = x + w
    y1 = y
    y2 = y + h

    k = cv2.waitKey(10) x00F
    if k == ord("p"):
         print ("diaginal point 1 (x1,y1) = ({},{})".format(x1, y1))
         print ("diaginal point 2 (x2,y2) = ({},{})".format(x2, y2))

所以每当你按“p”时,它都会打印点 1 和 2 的 x 和 y 坐标:) 为简单起见,我提到了 x1、y1、x2 和 y2。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    相关资源
    最近更新 更多