<jupyter_start><jupyter_text>@Copyright (C): 2010-2019, Shenzhen Yahboom Tech  @Author: Malloy.Yuan  @Date: 2019-07-17 10:10:02  @LastEditors: Malloy.Yuan  @LastEditTime: 2019-09-17 17:54:19    Import related packages to create camera instances<jupyter_code>from jetbot import Camera
from jetbot import bgr8_to_jpeg
import PID
camera = Camera.instance(width=720, height=720)<jupyter_output><empty_output><jupyter_text>Create related control variables<jupyter_code>global face_x, face_y, face_w, face_h
face_x = face_y = face_w = face_h = 0
global target_valuex
target_valuex = 2048
global target_valuey
target_valuey = 2048<jupyter_output><empty_output><jupyter_text>Create a PID control instance<jupyter_code>xservo_pid = PID.PositionalPID(1.9, 0.3, 0.35)
yservo_pid = PID.PositionalPID(1.5, 0.2, 0.3)<jupyter_output><empty_output><jupyter_text>Create a PTZ servo engine instance<jupyter_code>from servoserial import ServoSerial
servo_device = ServoSerial()<jupyter_output><empty_output><jupyter_text>Create display Control<jupyter_code>import traitlets
import ipywidgets.widgets as widgets
from IPython.display import display
face_image = widgets.Image(format='jpeg', width=300, height=300)
display(face_image)<jupyter_output><empty_output><jupyter_text>Load "Haar" Cascade Classifier<jupyter_code>import cv2
face_cascade = cv2.CascadeClassifier('123.xml')<jupyter_output><empty_output><jupyter_text>Main process of PTZ movement<jupyter_code>while 1:
    frame = camera.value
    frame = cv2.resize(frame, (300, 300))
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale( gray )
    if len(faces)>0:
        (face_x, face_y, face_w, face_h) = faces[0]
        # Mark the detected face
        # cv2.rectangle(frame,(face_x,face_y),(face_x+face_h,face_y+face_w),(0,255,0),2)
        cv2.rectangle(frame,(face_x+10,face_y),(face_x+face_w-10,face_y+face_h+20),(0,255,0),2)

        # Proportion-Integration-Differentiation algorithm
        # Input X-axis direction parameter PID control input
        xservo_pid.SystemOutput = face_x+face_h/2
        xservo_pid.SetStepSignal(150)
        xservo_pid.SetInertiaTime(0.01, 0.006)
        target_valuex = int(2048 + xservo_pid.SystemOutput)
        # Input Y axis direction parameter PID control input
        yservo_pid.SystemOutput = face_y+face_w/2
        yservo_pid.SetStepSignal(150)
        yservo_pid.SetInertiaTime(0.01, 0.006)
        target_valuey = int(2048+yservo_pid.SystemOutput)
        # Rotate the gimbal to the PID adjustment position
        servo_device.Servo_serial_double_control(1, target_valuex, 2, target_valuey)
    # Real-time return of image data for display
    face_image.value = bgr8_to_jpeg(frame)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09978150036416605, 'fraction_numerical': 0.051711580480699196, 'mean_word_length': 5.358796296296297, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '272558', 'license': 'MIT', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1004, 'n_words': 180, 'path': 'notebooks/11.Face tracking/Face tracking .ipynb', 'repo_name': 'Hoehlenbaer/jetbot', 'size': '2746'}