AIServoPlatform
This Project is base on the raspberry hardware platform which be used for automatic face track and also person track filed in the future.
- AI Tech.
- Raspberry Programming.
- Hardware Update.
1. Stove Control Code
import pygame
from pygame.locals import *
import RPi.GPIO as GPIO
from sys import exit
import time
import sys
import os
if pygame.font is None:
print("The font of pygame is not aviliable!")
exit()
else:
print("You can use the module named font!")
def show_text(surface_handle, pos, text, color, font_bold = False, font_size = 13, font_italic = False):
cur_font = pygame.font.SysFont('arial', font_size)
cur_font.set_bold(font_bold)
cur_font.set_italic(font_italic)
text_fmt = cur_font.render(text, 1, color)
surface_handle.blit(text_fmt, pos)
PWM_Pinx = 14
PWM_Piny = 15
PWM_Frexy = 50
PWM_X_Duty = 5.0
PWM_Y_Duty = 5.0
GPIO.setmode(GPIO.BCM)
GPIO.setup(PWM_Pinx,GPIO.OUT)
GPIO.setup(PWM_Piny,GPIO.OUT)
GPIO.setwarnings(False)
px = GPIO.PWM(PWM_Pinx,PWM_Frexy)
py = GPIO.PWM(PWM_Piny,PWM_Frexy)
backgroundimage = 'background.jpg'
mouseimage = 'mouse.jpg'
pygame.init()
screen = pygame.display.set_mode((576, 576), 0, 32)
pygame.display.set_caption('Stove Control!')
background = pygame.image.load(backgroundimage).convert()
mouse_course = pygame.image.load(mouseimage).convert_alpha()
mouse_width_half = mouse_course.get_width()/2
mouse_height_half = mouse_course.get_height()/2
param1 = float(287.0)/90.0
param2 = float(10.0)/182.0
px.start(5.0)
py.start(5.0)
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0, 0))
x, y = pygame.mouse.get_pos()
x -= mouse_width_half
y -= mouse_height_half
screen.blit(mouse_course, (x, y))
V_degree = (float(y) - 286.0)/param1
H_degree = (float(x) - 286.0)/param1
text_pos = u"Position:(%.2f,%.2f)" % (V_degree, H_degree)
# print(text_pos)
show_text(screen,(410,10),text_pos,(0,0,255),False,13,True)
pygame.display.update()
PWM_X_Duty = 2.5+(H_degree+91-40)*param2
PWM_Y_Duty = 2.5+(V_degree+91-40)*param2
px.ChangeDutyCycle(PWM_X_Duty)
py.ChangeDutyCycle(PWM_Y_Duty)
px.stop()
py.stop()
GPIO.cleanup()
2. Video RealTime Sample
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
Height = 150
Width = 105
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (Height,Width)
camera.framerate = 30
camera.hflip = False
camera.vflip = False
rawCapture = PiRGBArray(camera, size=(Height,Width))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow("Frame",image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# cv2.imshow("Frame",gray)
ret,mask = cv2.threshold(gray,175,255,cv2.THRESH_BINARY)
# cv2.imshow("Frame", mask)
Height,Width = mask.shape
Start_Height = 0
Start_Flag = 0
Height_Count = 0
Start_Width = Width-1
NoPixel = 1
for i in range(Height):
for j in range(Width):
if mask[i][j] != 0:
if Start_Flag == 0:
Start_Height = i
Start_Flag = 1
if Start_Width >j:
Start_Width = j
Height_Count = Height_Count + 1
NoPixel = 0
break
else:
NoPixel = 1
print(Start_Height,Start_Width,Height_Count)
Start_Height += Height_Count/2
Position_One = [Start_Height,Start_Width+Height_Count/2]
Position_Two = [Start_Height,Start_Width+Height_Count]
Position_Thr = [Start_Height,Start_Width+Height_Count*3/2]
Color_One = image[Position_One[0]][Position_One[1]]
Color_Two = image[Position_Two[0]][Position_Two[1]]
Color_Thr = image[Position_Thr[0]][Position_Thr[1]]
print(Color_One)
print(Color_Two)
print(Color_Thr)
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