def main(): detect = MaskDetect(json_path=j_path, weight_path=w_path) camera = cv2.VideoCapture(CAMERA) fr = FaceRecognition(known_folder="faces/", callback=callback) cv2.namedWindow("re_image") count = 0 begin = 0 # 每过5秒,尝试把识别数据写入数据库 recond_timer = CarTimer(5) while True: begin = time.perf_counter() ret, frame = camera.read() # 读取每一帧 test_frame = cv2.resize(frame, (detect_width, detect_height)) faces = detect.detect(test_frame) if faces: no_masks = [one for one in faces if one[0] == 1] if no_masks: if timer.timeout(): for one in no_masks: re_image = get_face(frame, one) fr.recognition(re_image) cv2.imshow("re_image", re_image) timer.restart() cv2.imshow("testWindow", frame) # 把帧显示在名字为testWindow的窗口中 frame_rate = 1 / (time.perf_counter() - begin) # print("frame_rate:{}".format(frame_rate)) # 检测键盘,发现按下 q 键 退出循环 if cv2.waitKey(1) == ord('q'): break if recond_timer.timeout(): write_db() recond_timer.restart() fr.close() camera.release() # 释放摄像头 cv2.destroyAllWindows() # 关闭所有窗口
speed = _map(max_width, 50, 400, 150, 80) serial.drive_motor(int(speed), int(speed)) timer3.restart() # 计时没有结束之前一直循环 while not is_sigint_up: # get_objects函数返回的是包含0个以上的Object对象列表, # 如果列表中有对象存在,那么迭代循环 打印对象的属性 # if targets: # for obj in targets: # print("发现对象 id:{},名称:{},面积:{},高度:{},宽度:{}" # .format(obj.class_id, obj.chinese, obj.area, obj.height, obj.width)) power = False targets = recognition.get_objects() persons = [person for person in targets if person.class_id == 1] if persons: timer.restart() motor_controller(persons) servo_controller(persons) else: if timer.timeout(): serial.drive_motor(0, 0) # 循环结束必须调用close()函数,结束识别窗口,否则窗口将一直打开 recognition.close() serial.drive_motor(0, 0) serial.close()
from car.car_controller import BaseControl # 新建串口通信对象,除非测试,不要直接调用此类,控制小车应该通过CarController类 serial = CarSerial("/dev/ttyACM0", receive=True) # 参数为串口文件 # 新建一个CarController,传入串口通信对象,用于控制小车的各种动作 controller = CarController(serial, base_speed=100) # 新建一个计时器对象,设定他的计时时间为30秒 timer = CarTimer(interval=20) # 创建一个列表用于存储马达动作组合的列表 control_list = [] # 按需要控制的顺序,添加各种马达速度和执行时间 control_list.append(BaseControl(100, 100, 5)) # 直走5秒 control_list.append(BaseControl(0, 150, 2)) # 左转 2秒 control_list.append(BaseControl(0, 0, 2)) # 暂停2秒 control_list.append(BaseControl(150, 0, 2)) # 右转2秒 control_list.append(BaseControl(-100, -100, 5)) # 后退5秒 control_list.append(BaseControl(0, 0, 2)) # 停车 controller.group(control_list) # 当时间未到时循环 while not timer.timeout(): controller.update() # CarController的update方法必须在每次循环中调用,才能更新任务列表 time.sleep(0.05) # 模拟每秒20帧 # 计时时间到,控制小车停止 controller.exit()
import time import sys sys.path.append("..") from car.car_timer import CarTimer from car.car_controller import CarController from v_serial import VSerial timer1 = CarTimer(30) timer2 = CarTimer(5) serial = VSerial() control = CarController(car_serial=serial) control.go_straight(delay_time=5) # 直走5秒 while not timer1.timeout(): print("timer2.dur:{}".format(timer2.duration())) if timer2.timeout(): control.turn(direction=True, delay_time=1.5) # 左转1秒 control.go_straight(delay_time=5) timer2.restart() print("timer2.timeout") timer2.restart() control.update() time.sleep(0.05)