入驻于 2019-11-06 / MAKE 主页
@RaspiSQH 应该是电不够
import cv2
import numpy as np
import os
import RPi.GPIO as GPIO
import time
def tonum(num):
fm=10.0/180.0
num = num*fm + 2.5
num = int(num * 10)/10.0
return num
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
p25 = GPIO.PWM(25, 50)
p25.start(tonum(90))
#p25.ChangeDutyCycle(0)
time.sleep(0.02)
# 作用区
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
font = cv2.FONT_HERSHEY_SIMPLEX
#iniciate id counter
id = 0
# names related to ids: example ==> Marcelo: id=1, etc
names = ['None', 'Yan', 'Pan', 'Ilza', 'Z', 'W']
# Initialize and start realtime video capture
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video widht
cam.set(4, 480) # set video height
# Define min window size to be recognized as a face
minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)
# 角度
angle = 90
# 次数
index = 0
temp = 0
# shijian
t0 = time.time()
t1 = 0
while True:
ret, img =cam.read()
img = cv2.flip(img, -1) # Flip vertically
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 5,
minSize = (int(minW), int(minH)),
)
for(x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
# Check if confidence is less them 100 ==> "0" is perfect match
if (confidence < 100):
id = names[id]
confidence = " {0}%".format(round(100 - confidence))
# 五秒为间隔
if ((t1 -t0) >= 5) | (y == 0) :
if id in names:
if angle == 90:
p25.ChangeDutyCycle(tonum(0))
time.sleep(0.02)
p25.ChangeDutyCycle(0)
time.sleep(0.2)
angle = 0
else:
p25.ChangeDutyCycle(tonum(90))
time.sleep(0.02)
p25.ChangeDutyCycle(0)
time.sleep(0.2)
angle = 90
index += 1
if index > temp:
t0 = time.time()
temp = index
t1 = round(time.time())
else:
id = "unknown"
confidence = " {0}%".format(round(100 - confidence))
cv2.putText(img, str(id), (x+5,y-5), font, 1, (255,255,255), 2)
cv2.putText(img, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1)
cv2.imshow('camera',img)
#p25.ChangeDutyCycle(tonum(num))
k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
if k == 27:
break
# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()
/****************************************************************/
这是后来我改的代码,加了时间间隔为5s左右,就是y -x,让舵机转一下,次数为index变量,但是后来舵机突然开始抖起来了。。。。。。。。。。感觉应该是对的。
@Spoony 改成单线程后有动静了,但是有点慢。谢谢。
@Spoony 在htop显示中显示的状态符号为D,进程的启动命令名称为
【/usr/lib/gcc/arm-linux-gnueabihf/8/cc1plus -quiet -I /home/pi/opencv/build -I /home/pi/opencv/modules/python/python3/include -I /home/pi/opencv/build/modules/python3 -I /home/pi/opencv/modules/core/include -I /home/pi/opencv/modules/flam/include -I /……】
省略号后的看不到了;
swap一直在增加。绿灯一直亮,红灯有时不亮。
@Spoony 按照你的做法还是会有一样的报错,后来make时,提示无makefile文件,重新cmake也会出错,在网上查找了一些方法之后还是会报错。于是删除了opencv_contrib重新安装,make时没有了错误,但是在100%building文件时,树莓派总是卡住,这是属于正常现象吗,等它自己好吗?