def signal_handler(sig, frame): print("[INFO] You pressed `ctrl + c`! Exiting...") pth.servo_enable(1, False) pth.servo_enable(2, False) sys.exit()
def signal_handler(sig, frame): # print a status message print("[INFO] You pressed `ctrl + c`! Exiting...") # disable the servos pth.servo_enable(1, False) pth.servo_enable(2, False) # exit sys.exit()
def interrupt_handler(sig, frame): """ This is a function to handle system interrupts for signals """ # prints system message print("[MESSAGE] You cancelled the system process! Now Exiting...") # disable the servos # DOC: servo_enable has two arguments: servo number, state (True/False) pth.servo_enable(1, False) pth.servo_enable(2, False) # exit program sys.exit()
def pantilt_process_manager(model_cls, labels=('person', ), rotation=0): pth.servo_enable(1, True) pth.servo_enable(2, True) with Manager() as manager: # set initial bounding box (x, y)-coordinates to center of frame center_x = manager.Value('i', 0) center_y = manager.Value('i', 0) center_x.value = RESOLUTION[0] // 2 center_y.value = RESOLUTION[1] // 2 # pan and tilt angles updated by independent PID processes pan = manager.Value('i', 0) tilt = manager.Value('i', 0) # PID gains for panning pan_p = manager.Value('f', 0.05) # 0 time integral gain until inferencing is faster than ~50ms pan_i = manager.Value('f', 0.1) pan_d = manager.Value('f', 0) # PID gains for tilting tilt_p = manager.Value('f', 0.15) # 0 time integral gain until inferencing is faster than ~50ms tilt_i = manager.Value('f', 0.2) tilt_d = manager.Value('f', 0) detect_processr = Process(target=run_pantilt_detect, args=(center_x, center_y, labels, model_cls, rotation)) pan_process = Process(target=pid_process, args=(pan, pan_p, pan_i, pan_d, center_x, CENTER[0], 'pan')) tilt_process = Process(target=pid_process, args=(tilt, tilt_p, tilt_i, tilt_d, center_y, CENTER[1], 'tilt')) servo_process = Process(target=set_servos, args=(pan, tilt)) detect_processr.start() pan_process.start() tilt_process.start() servo_process.start() detect_processr.join() pan_process.join() tilt_process.join() servo_process.join()
def main(): print('Initializing HAT') hat.servo_enable(1, False) hat.servo_enable(2, False) hat.light_mode(hat.WS2812) hat.light_type(hat.GRBW) print('Initializing camera') with picamera.PiCamera() as camera: camera.resolution = (WIDTH, HEIGHT) camera.framerate = FRAMERATE camera.vflip = VFLIP # flips image rightside up, as needed camera.hflip = HFLIP # flips image left-right, as needed sleep(1) # camera warm-up time print('Initializing websockets server on port %d' % WS_PORT) WebSocketWSGIHandler.http_version = '1.1' websocket_server = make_server( '', WS_PORT, server_class=WSGIServer, handler_class=WebSocketWSGIRequestHandler, app=WebSocketWSGIApplication(handler_cls=StreamingWebSocket)) websocket_server.initialize_websockets_manager() websocket_thread = Thread(target=websocket_server.serve_forever) print('Initializing HTTP server on port %d' % HTTP_PORT) http_server = StreamingHttpServer() http_thread = Thread(target=http_server.serve_forever) print('Initializing broadcast thread') output = BroadcastOutput(camera) broadcast_thread = BroadcastThread(output.converter, websocket_server) print('Starting recording') camera.start_recording(output, 'yuv') try: print('Starting websockets thread') websocket_thread.start() print('Starting HTTP server thread') http_thread.start() print('Starting broadcast thread') broadcast_thread.start() while True: camera.wait_recording(1) except KeyboardInterrupt: pass finally: print('Stopping recording') camera.stop_recording() print('Waiting for broadcast thread to finish') broadcast_thread.join() print('Shutting down HTTP server') http_server.shutdown() print('Shutting down websockets server') websocket_server.shutdown() print('Waiting for HTTP server thread to finish') http_thread.join() print('Disabling servos') hat.servo_enable(1, False) hat.servo_enable(2, False) hat.clear() hat.show() print('Waiting for websockets thread to finish') websocket_thread.join()
# System run: if __name__ == '__main__': # read in cmd line arguments through arg parser ap = argparse.ArgumentParser() ap.add_argument("-c", "--cascade", type=str, required=True, help="path to input Haar cascade for face detection") args = vars(ap.parse_args()) # parallel threading manager for thread-safe processes with Manager() as manager: # start the servos pth.servo_enable(1, True) pth.servo_enable(2, True) # set default integer value for calibration of camera's x,y coords centerX = manager.Value("i", 0) centerY = manager.Value("i", 0) # set default integer value for calibration of object x,y coords objX = manager.Value("i", 0) objY = manager.Value("i", 0) # set pan and tilt integer values will be managed by independed PIDs pan = manager.Value("i", 0) tlt = manager.Value("i", 0) # set PID float values for panning
def do_GET(self): url = urlparse(self.path) if url.path == '/': self.send_response(301) self.send_header('Location', '/index.html') self.end_headers() return elif url.path == '/do_orient': try: data = {k: int(v[0]) for k, v in parse_qs(url.query).items()} except (IndexError, ValueError) as e: self.send_error(400, str(e)) else: with self.server.hat_lock: hat.servo_enable(1, True) hat.servo_enable(2, True) try: delay = 0.1 if 'pan' in data: pan = -data['pan'] delay = max( delay, 0.5 * (abs(pan - self.server.last_pan) / 180)) hat.pan(pan) self.server.last_pan = pan if 'tilt' in data: tilt = -data['tilt'] delay = max( delay, 0.5 * (abs(tilt - self.server.last_tilt) / 180)) hat.tilt(tilt) self.server.last_tilt = tilt # Wait for the servo to complete its sweep sleep(delay) finally: hat.servo_enable(1, False) hat.servo_enable(2, False) self.send_response(200) self.end_headers() return elif url.path == '/do_light': try: data = { int(k): (int(r), int(g), int(b), int(w)) for k, v in parse_qs(url.query).items() for r, g, b, w in (v[0].split(',', 3), ) } except (IndexError, ValueError) as e: self.send_error(400, str(e)) else: if -1 in data: r, g, b, w = data.pop(-1) for i in range(8): data[i] = r, g, b, w with self.server.hat_lock: for index, (r, g, b, w) in data.items(): if 0 <= index < 8: hat.set_pixel_rgbw(index, r, g, b, w) hat.show() self.send_response(200) self.end_headers() return elif url.path == '/jsmpg.js': content_type = 'application/javascript' content = self.server.jsmpg_content elif url.path == '/index.html': content_type = 'text/html; charset=utf-8' tpl = Template(self.server.index_template) content = tpl.safe_substitute( dict(WS_PORT=WS_PORT, WIDTH=WIDTH, HEIGHT=HEIGHT, COLOR=COLOR, BGCOLOR=BGCOLOR)) elif url.path == '/styles.css': content_type = 'text/css; charset=utf-8' tpl = Template(self.server.styles_template) content = tpl.safe_substitute( dict(WS_PORT=WS_PORT, WIDTH=WIDTH, HEIGHT=HEIGHT, COLOR=COLOR, BGCOLOR=BGCOLOR)) else: self.send_error(404, 'File not found') return content = content.encode('utf-8') self.send_response(200) self.send_header('Content-Type', content_type) self.send_header('Content-Length', len(content)) self.send_header('Last-Modified', self.date_time_string(time())) self.end_headers() if self.command == 'GET': self.wfile.write(content)
def init(): pt.servo_enable(1, True) pt.servo_enable(2, True) pt.tilt(10) pt.pan(-90)
def end(): pt.servo_enable(1, False) pt.servo_enable(2, False)
def close(): print("Shutting servos down") pantilthat.servo_enable(1, False) pantilthat.servo_enable(2, False)
def pantilt_process_manager(use_tpu): # Enable the servos pth.servo_enable(1, True) pth.servo_enable(2, True) # Main logic loop with Manager() as manager: #-------------------------------------------------------------- # Create variables that will be shared by the various processes # X and Y value of the bounding box. Type = integer center_x = manager.Value('i', 0) center_y = manager.Value('i', 0) center_x.value = CENTER[0] center_y.value = CENTER[1] # Pan and tilt values for the camera. Type = integer pan = manager.Value('i', 0) tilt = manager.Value('i', 0) # PID gains for panning. Type = float pan_p = manager.Value('f', 0.005) pan_i = manager.Value('f', 0.01) pan_d = manager.Value('f', 0) # PID gains for tilting. Type = float tilt_p = manager.Value('f', 0.075) tilt_i = manager.Value('f', 0.01) tilt_d = manager.Value('f', 0) # -------------------------------------------------------- # Create the processes that will run the various functions # Graphics process, includes the CNN detect_process = Process(target=capture_and_detect, args=(center_x, center_y, use_tpu)) # Process for panning (move right-left) pan_process = Process(target=pid_process, args=(pan, pan_p, pan_i, pan_d, center_x, CENTER[0], 'pan')) # Process for tilting (move up-down) tilt_process = Process(target=pid_process, args=(tilt, tilt_p, tilt_i, tilt_d, center_y, CENTER[1], 'tilt')) # Process for moving servos servo_process = Process(target=set_servos, args=(pan, tilt)) #-------------------- # Start all processes detect_process.start() pan_process.start() tilt_process.start() servo_process.start() #------------------- # Join all processes detect_process.join() pan_process.join() tilt_process.join() servo_process.join()
import cv2 import numpy as np import pantilthat cap = cv2.VideoCapture(0) pantilthat.servo_enable(1, True) pantilthat.servo_enable(2, True) pantilthat.servo_one(0) pantilthat.servo_two(0) while True: _, frame = cap.read() hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # red color low_red = np.array([161, 155, 84]) high_red = np.array([179, 255, 255]) red_mask = cv2.inRange(hsv_frame, low_red, high_red) ##Detect biggest red object contours, _ = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True) #draw rectangle on biggest contour for cnt in contours: (x, y, w, h) = cv2.boundingRect(cnt) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)