1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| import cv2 import ipywidgets.widgets as widgets from Raspblock import Raspblock
robot = Raspblock() import cv2
face_haar = cv2.CascadeClassifier('haarcascade_profileface.xml')
image_widget = widgets.Image(format='jpeg', width=320, height=240) display(image_widget)
import cv2
def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])
import ipywidgets as widgets
XServo_P = widgets.FloatSlider( value=1.1, min=0, max=10.0, step=0.1, description='XServo-P:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', )
XServo_I = widgets.FloatSlider( value=0.2, min=0, max=10.0, step=0.1, description='XServo-I:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', )
XServo_D = widgets.FloatSlider( value=0.8, min=0, max=10.0, step=0.1, description='XServer-D:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', )
YServo_P = widgets.FloatSlider( value=0.8, min=0, max=10.0, step=0.1, description='YServo-P:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', )
YServo_I = widgets.FloatSlider( value=0.2, min=0, max=10.0, step=0.1, description='YServo-I:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', )
YServo_D = widgets.FloatSlider( value=0.8, min=0, max=10.0, step=0.1, description='YServer-D:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', ) display(XServo_P, XServo_I, XServo_D, YServo_P, YServo_I, YServo_D)
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
import PID
xservo_pid = PID.PositionalPID(XServo_P.value, XServo_I.value, XServo_D.value) yservo_pid = PID.PositionalPID(YServo_P.value, YServo_I.value, YServo_D.value)
image = cv2.VideoCapture(0) image.set(3, 320) image.set(4, 240)
while 1:
ret, frame = image.read() try: image_widget.value = bgr8_to_jpeg(frame) except: continue gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_haar.detectMultiScale(gray_img, 1.1, 3)
xservo_pid = PID.PositionalPID(XServo_P.value, XServo_I.value, XServo_D.value) yservo_pid = PID.PositionalPID(YServo_P.value, YServo_I.value, YServo_D.value)
if len(faces) > 0: (face_x, face_y, face_w, face_h) = faces[0] cv2.rectangle(frame, (face_x, face_y), (face_x + face_w, face_y + face_h), (0, 255, 0), 2) try: image_widget.value = bgr8_to_jpeg(frame) except: continue
xservo_pid.SystemOutput = face_x + face_w / 2 xservo_pid.SetStepSignal(150) xservo_pid.SetInertiaTime(0.01, 0.1) target_valuex = int(1500 + xservo_pid.SystemOutput)
yservo_pid.SystemOutput = face_y + face_h / 2 yservo_pid.SetStepSignal(120) yservo_pid.SetInertiaTime(0.01, 0.1) target_valuey = int(1500 - yservo_pid.SystemOutput)
robot.Servo_control(target_valuex, target_valuey)
|