前言
PC端连接WiFi模块产生的局域网,并用python把WiFi模块摄像头的视频流爬取下来。最后,对该视频进行实时人脸检测。一旦检测到人脸,则通过XBee向树莓派发送数据。树莓派接收到数据后,驱动步进电机运行。
PC端python代码如下
from digi.xbee.devices import XBeeDevice
import cv2
PORT = "COM3"
BAUD_RATE = 9600
DATA_TO_SEND = "1"
REMOTE_NODE_ID = "XBEE_A"
cap = cv2.VideoCapture("http://192.168.1.1:8080/?action=stream")
def main():
while (True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
xmlfile = 'C:\\Users\\13161\\PycharmProjects\\untitled1\\venv\\Lib\\site-packages\\cv2\data\\haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier(xmlfile)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.15,
minNeighbors=5,
minSize=(5, 5),
)
print("发现{0}个目标!".format(len(faces)))
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + w), (0, 255, 0), 2)
cv2.imshow("frame", frame)
aaa=[0,0,0]
aaa.append(len(faces))
#xxx +=len(faces)
if max(aaa)==1:
print(111111111)
device = XBeeDevice(PORT, BAUD_RATE)
try:
device.open()
# Obtain the remote XBee device from the XBee network.
xbee_network = device.get_network()
remote_device = xbee_network.discover_device(REMOTE_NODE_ID)
if remote_device is None:
print("Could not find the remote device")
exit(1)
print("Sending data to %s >> %s..." % (remote_device.get_64bit_addr(), DATA_TO_SEND))
device.send_data(remote_device, DATA_TO_SEND)
print("Success")
finally:
if device is not None and device.is_open():
device.close()
# Display the resulting frame
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
树莓派端代码如下
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import serial
delay=2 #delay 2ms
pin_4 = 4
pin_17 = 17
pin_23 = 23
pin_24 = 24
GPIO.setmode(GPIO.BCM) #设置引脚的编码方式
def init():
GPIO.setwarnings(False)
GPIO.setup(pin_4, GPIO.OUT)
GPIO.setup(pin_17, GPIO.OUT)
GPIO.setup(pin_23, GPIO.OUT)
GPIO.setup(pin_24, GPIO.OUT)
def forward(delay):
setStep(1, 0, 0, 0)
time.sleep(delay)
setStep(0, 1, 0, 0)
time.sleep(delay)
setStep(0, 0, 1, 0)
time.sleep(delay)
setStep(0, 0, 0, 1)
time.sleep(delay)
def setStep(w1, w2, w3, w4):
GPIO.output(pin_4, w1)
GPIO.output(pin_17, w2)
GPIO.output(pin_23, w3)
GPIO.output(pin_24, w4)
def main():
init()
ser=serial.Serial('/dev/ttyUSB0',9600)
x= 'n'
y= 'n'
while x !='q':
x=ser.read(1)
print(x)
if x!=y:
while True:
forward(int(delay) / 1000.0)
main() # 调用main