[PYTHON] [Hinweis] (Zupai) Gesichtsverfolgung mit Pan-Tilt-Hut und Kamera

Überblick

pi@raspberrypi:~ $ python Python 2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.

from pantilthat import * angle_pan = 15 #Winkel:-90 - +90 angle_tilt = -30 #Winkel:-90 - +90 pan(angle_pan) tilt(angle_tilt) quit() pi@raspberrypi:~ $


## Umgebung
- Raspberry Pi 2 / 3
 - Kameramodul V2.1 (funktionierte auch mit V1.3)
 --Raspbian: `2017-01-11-raspbian-jessie.img` (OpenCV zeigt das Kamerabild im Fenster an, es ist also nicht Lite)

## Verfahren
 1. Zusammenbau des Pan-Tilt-Kits [Zusammenbau des Pan-Tilt-HAT](https://learn.pimoroni.com/tutorial/sandyj/assembling-pan-tilt-hat)
 https://learn.pimoroni.com/tutorial/sandyj/assembling-pan-tilt-hat <br> Ich frage mich, ob ich das tun kann, während ich mir die Bilder ansehe.
 --Pan-Tilt-Modul an der HAT-Karte befestigt
 ――Es ist eine gute Idee, das Ende der Nylonschraube mit einer Schere oder Blechschere zu kürzen, da die schwarze Nylonschraube lang ist.
 - Stecken Sie das Servomotorkabel in den hinteren Anschluss (GND = braun, 5V = rot, PWM-gelb).
 --Pan (Schwert schütteln) auf Kanal 1 (SERVO1)
 - Kippen (Hals senkrecht schütteln) auf Kanal 2 (SERVO2)
 - Montage der Kamera-Befestigungsbasis
 ――Ich denke, dass die Kerbe auf der Vorderseite zwischen V1.3 und V2.1 unterschiedlich ist.
 ――Die weiße Schraube ist länger, damit Sie sie schneiden können. (Auch hier ist es eine gute Idee, den überschüssigen Nylonbolzen mit einer Schere oder einer Blechschere abzuschneiden.)
 - Bringen Sie die Kamera-Befestigungsbasis am Pan-Tilt-Modul an
 - Das Kamerakabel geht hoch
 --Neopixel Stick ... Ich habe es nicht gekauft, also überspringe es.
 - Stecken Sie das Kamerakabel vollständig ein.
 ―― Etwa 1 mm zum Anheben der Teile (braun oder schwarz?) Über den Raspeltortenauslass. Wenn Sie zu viel Kraft aufbringen, wird es brechen.
 - Stecken Sie das Kamerakabel ein
 ――Wenn Sie es anheben und hineinschieben, ist es fixiert.
 - Setzen Sie den Hut in die 40-polige Raspeltorte ein.

 2. Vorbereitung des Boot-Systems (üblich)
 - Raspbian
 --` sudo apt-get update && sudo apt-get upgrade -y` # Paketupdate

 3. Installieren Sie Pan-Tilt-bezogene Pakete
`curl -sS https://get.pimoroni.com/pantilthat | bash`
 Wenn Sie etwas gefragt werden, "y [Enter]"

 4. Kameramodul aktivieren <br> `sudo raspi-config nonint do_camera 0 # 0: Aktivieren / 1: Deaktivieren`


 5. `sudo reboot` # reboot
 6. Überprüfen Sie die Funktion des PanTilt-Moduls
 --Öffnen Sie das Terminal,
 --Starten Sie Python mit `Python [Enter]`
 ――Nachher kopieren
 
		```py
		from pantilthat import *
		angle_pan  = 15   #Winkel:-90 - +90
		angle_tilt = -30  #Winkel:-90 - +90
		pan(angle_pan)
		tilt(angle_tilt)
		quit()
		```
 -Funktioniert es? Wenn Sie die Zahl ändern, ändert sich die Richtung. Angle_pan = 0 / angle_tilt = 0 und Sie haben das Gefühl, nach vorne zu schauen?



### Demo-Programm zur Gesichtsverfolgung
https://github.com/pimoroni/PanTiltFacetracker


 1. Installieren Sie die erforderlichen Pakete <br> `sudo apt-get install python-smbus python-opencv opencv-data`
2. git clone 

    ```bash
git clone https://github.com/pimoroni/PanTiltFacetracker
cd PanTiltFacetracker
  1. Führen Sie
    . / Facetracker_lbp.py aus

Andere

--Log

```shell-session:
pi@raspberrypi:~ $ curl -sS https://get.pimoroni.com/pantilthat | bash

This script will install everything needed to use
Pan-Tilt HAT

Always be careful when running scripts and commands
copied from the internet. Ensure they are from a
trusted source.

If you want to see what this script does before
running it, you should run:
\curl -sS https://get.pimoroni.com/pantilthat

Note: Pan-Tilt HAT requires I2C communication

Do you wish to continue? [y/N] y

Checking environment...
Updating apt indexes...
.........
Reading package lists...
.........

Checking hardware requirements...

I2C must be enabled for Pan-Tilt HAT to work
I2C is now enabled

Checking packages required by I2C interface...

Pan-Tilt HAT comes with examples and documentation that you may wish to install.
Performing a full install will ensure those resources are installed,
along with all required dependencies. It may however take a while!

Do you wish to perform a full install? [y/N] y
Checking install requirements...

Checking for dependencies...

Installing python-pantilthat...
install ok installed

Installing python3-pantilthat...
install ok installed


Checking for additional software...

python-picamera is already installed
python3-picamera is already installed

Downloading examples and documentation...
Resources for your Pan-Tilt HAT were copied to
/home/pi/Pimoroni/pantilthat

All done!

Enjoy your Pan-Tilt HAT!

pi@raspberrypi:~ $ sudo reboot
```

――Ist die Farbe seltsam? Wenn der rote projiziert wird, wird er blau und der blaue wird rot. Ist die RGB-Ausrichtung falsch eingestellt? Kann v4l2 nicht über OpenCV eingestellt werden? CV_CAP_PROP_FORMAT ?? Ich weiß nicht, wie ich das beheben soll, deshalb habe ich es geändert, um PiCamera of Python zu verwenden.

- `sudo apt-get install python-picamera`

--Code, der für die Verwendung von Picamera durcheinander gebracht wurde.

	```py
	#!/usr/bin/env python
	## http://qiita.com/mt08/items/97d89a09e5129e10f88c

	from picamera.array import PiRGBArray
	from picamera import PiCamera
	import cv2, sys, time, os
	from pantilthat import *

	# Frame Size. Smaller is faster, but less accurate.
	# Wide and short is better, since moving your head
	# vertically is kinda hard!
	FRAME_W = 192
	FRAME_H = 112

	# Default Pan/Tilt for the camera in degrees.
	# Camera range is from -90 to 90
	cam_pan = 90
	cam_tilt = 60

	# Set up the CascadeClassifier for face tracking
	#cascPath = 'haarcascade_frontalface_default.xml' # sys.argv[1]
	cascPath = '/usr/share/opencv/lbpcascades/lbpcascade_frontalface.xml'
	faceCascade = cv2.CascadeClassifier(cascPath)

	# Set up the capture with our frame size
	camera = PiCamera()
	camera.resolution = (FRAME_W, FRAME_H)
	camera.framerate = 32
	rawCapture = PiRGBArray(camera, size=(FRAME_W, FRAME_H))
	time.sleep(0.1)

	# Turn the camera to the default position
	pan(cam_pan-90)
	tilt(cam_tilt-90)
	light_mode(WS2812)

	def lights(r,g,b,w):
		for x in range(18):
			set_pixel_rgbw(x,r if x in [3,4] else 0,g if x in [3,4] else 0,b,w if x in [0,1,6,7] else 0)
		show()

	lights(0,0,0,50)


	for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

		frame = image.array

		# This line lets you mount the camera the "right" way up, with neopixels above
		frame = cv2.flip(frame, -1)
		
		# Convert to greyscale for detection
		gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
		gray = cv2.equalizeHist( gray )

		# Do face detection
		faces = faceCascade.detectMultiScale(gray, 1.1, 3, 0, (10, 10))
	   
		# Slower method 
		'''faces = faceCascade.detectMultiScale(
			gray,
			scaleFactor=1.1,
			minNeighbors=4,
			minSize=(20, 20),
			flags=cv2.cv.CV_HAAR_SCALE_IMAGE | cv2.cv.CV_HAAR_FIND_BIGGEST_OBJECT | cv2.cv.CV_HAAR_DO_ROUGH_SEARCH
		)'''
		
		lights(50 if len(faces) == 0 else 0, 50 if len(faces) > 0 else 0,0,50)

		for (x, y, w, h) in faces:
			# Draw a green rectangle around the face
			cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

			# Track first face
			
			# Get the center of the face
			x = x + (w/2)
			y = y + (h/2)

			# Correct relative to center of image
			turn_x  = float(x - (FRAME_W/2))
			turn_y  = float(y - (FRAME_H/2))

			# Convert to percentage offset
			turn_x  /= float(FRAME_W/2)
			turn_y  /= float(FRAME_H/2)

			# Scale offset to degrees
			turn_x   *= 2.5 # VFOV
			turn_y   *= 2.5 # HFOV
			cam_pan  += -turn_x
			cam_tilt += turn_y

			print(cam_pan-90, cam_tilt-90)

			# Clamp Pan/Tilt to 0 to 180 degrees
			cam_pan = max(0,min(180,cam_pan))
			cam_tilt = max(0,min(180,cam_tilt))

			# Update the servos
			pan(int(cam_pan-90))
			tilt(int(cam_tilt-90))

			break

		frame = cv2.resize(frame, (540,300))
		frame = cv2.flip(frame, 1)
	   
		# Display the image, with rectangle
		# on the Pi desktop 
		cv2.imshow('Video', frame)
		key = cv2.waitKey(1) & 0xFF

		# clear the stream in preparation for the next frame
		rawCapture.truncate(0)

		# if the `q` key was pressed, break from the loop
		if key == ord("q"):
			break
		
	# When everything is done, release the capture
	cv2.destroyAllWindows()




 TODO
 ――Wollen Sie ein Sukusho oder ein Bewegungsvideo posten?
 -Ist es mit der Lachmann-Marke kompatibel?
 - Mehrere Gesichter erkennen und das größte verfolgen?






Recommended Posts

[Hinweis] (Zupai) Gesichtsverfolgung mit Pan-Tilt-Hut und Kamera
Gesichtserkennung mit Kamera mit opencv3 + python2.7
Hallo Welt- und Gesichtserkennung mit opencv-python 4.2
Hallo Welt- und Gesichtserkennung mit OpenCV 4.3 + Python
Erfassen Sie die Position und Ausrichtung der Kamera mit dem ArUco-Marker