def demo_step_find_object(ip, speed=20, dis=1, object='cup', vAngle=30, hAngle=90): """ 移动,一步一步的寻找物体 Parameter ------- *ip:string -树莓派的 *object:string -要寻找的物体 *vAngle:int -垂直方向的角度 *hAngle:int -水平方向的角度 *speed:int -移动的速度 *dis:float -时间间隔 """ # 初始化 car = Car(ip) # 相机初始化 camera = Camera() camera.connect_server(ip) camera.start_receive() camera.thread_play() ai = Ai(classes="./aiLib/coco/coco.names", config="./aiLib/coco/yolov3.cfg", weight="./aiLib/coco/yolov3.weights") car.turn_servo_camera_vertical(vAngle) car.turn_servo_camera_horizental(hAngle) global STOP_FLAGE while True: if STOP_FLAGE is True: print('over .............................................') return status = get_status_with_camera(car, camera, ai, object) car.turn_servo_camera_horizental(90) print(status) if status == 'status_move': car.run_forward(speed, dis) if status == 'status_turn_right': car.spin_right(10, 0.4) if status == 'status_turn_left': car.spin_left(10, 0.4) if status == 'status_stop': return
def demo_line_ai(ip, speed, dis): """ 利用训练数据进行巡线 Param ----- *ip: string -树莓的ip *speed: int -运行速度 *dis: int -检测一次走的距离 """ #相机初始化 camera = Camera() camera.connect_server(ip) camera.start_receive() # camera.thread_play() #车初始化 car = Car(ip) #Ai初始化,自己训练的结果 ai = Ai() global STOP_FLAGE while True: pic = camera.take_picture() #cv2.imread("./aiLib/line/line.jpg")# ret, name, box = ai.find_object(pic) cv2.imshow('test', ret) cv2.waitKey(1) # continue x = 0 if len(name) == 0: #没有检测到线 continue for item in name: if item == 'line': x = box[0][0] print(x, type(x)) if 250 < x < 380: car.run_forward(speed, dis) #直行 elif x < 250: print("spin right") car.turn_left(speed * 0.6, dis) #左转 elif x > 380: print("spin left") car.turn_right(speed * 0.6, dis) #右转 if STOP_FLAGE: return
def demo_line_algrithm(ip, speed, dis): """ 利用图像传统知识进行巡线,对图像进行二值化,选取组最大的轮廓的中心点。图像的长640,宽480。 0 左转 250 直行 320 直行 380 右转 640 Parameter -------- * ip:string -树莓派的IP * speed:int -小车运行速度 * dis:float -运行的时间,控制距离 """ #相机的初始化 camera = Camera() camera.connect_server(ip) camera.start_receive() camera.thread_play() #算法初始化 algithm = Algrithm() #小车初始化 car = Car(ip) global STOP_FLAGE while True: pic = camera.take_picture() pt = algithm.line(pic, 60) # algithm.set_debug() x = pt[0] print('%d和320比较' % x) if 250 < x < 380: car.run_forward(speed, dis) #直行 elif x < 250: print("spin left") car.turn_left(speed * 0.6, dis) #左转 elif x > 380: print("spin right") car.turn_right(speed * 0.6, dis) #右转 if STOP_FLAGE: return
def move_step_find_object1_thread(ip, camera, ai, object, vAngle, hAngle): """ 小车移动寻找目标 Parameter ------- *ip:string -树莓派的地址 *camera:class -相机控制 *ai:class -人工智能类 *object:string --目标名 *vAngle:int --相机垂直角度 *hAngle:int --相机水平角度 Return --------- None """ global CRUSING_FLOG global STOP_FLAGE global CAMERA_FLAGE car = Car(ip) car.turn_servo_camera_vertical(vAngle) car.turn_servo_camera_horizental(hAngle) #运动到相机指定方向 while not STOP_FLAGE: Cruising(car, 5) # 以7的速度进行漫游 car.turn_servo_camera_vertical(vAngle - 2) #抬起相机查看 car.turn_servo_camera_horizental(hAngle) ret = check_object_step(camera, car, ai, object) #旋转小车确认 if ret == 'find': break if ret == 'nothing': #没有找到,从新找 mainThread_ = threading.Thread(target=find_object, args=( camera, ai, object, )) mainThread_.start() CRUSING_FLOG = True car.spin_left(2, 0.1) # 回转,减少误差 start_sensor_check = False while True: if STOP_FLAGE == True: car.stop_all_wheels() print( 'Cruising over .............................................') return pic = camera.take_picture() y = [] x = [] x1 = 320 y1 = 0 for i in range(1): #多张图寻找 ret, names, box = ai.find_object(pic) for item in names: if item == object: id = names.index(item) print(box[id]) y.append(box[id][1]) x.append(box[id][0]) #多张图求平均 ysum = 0 for d in y: ysum = ysum + d if len(y) > 0: y1 = ysum / len(y) print(y1) #多张图,求取x平均 xsum = 0 for d in x: xsum = xsum + d if len(x) > 0: x1 = xsum / len(x) print(x1) if x1 > 360: #右转 print('turn right') car.turn_right(4, 0.1) elif x1 < 200: #左转 print('turn left') car.turn_left(4, 0.1) else: #直行 car.run_forward(4, 0.2) if y1 > 300: #说明车子靠近目标,启动超声波,相机向下移动一点 start_sensor_check = True car.turn_servo_camera_vertical(vAngle + 2) #超声波检查 dis1 = 1000 dis2 = 1000 if start_sensor_check: car.turn_servo_ultrasonic_angle(50) #左 time.sleep(0.2) dis1 = car.distance_from_obstacle() car.turn_servo_ultrasonic_angle(130) #右 time.sleep(0.2) dis2 = car.distance_from_obstacle() car.turn_servo_ultrasonic_angle(90) #中 time.sleep(0.2) dis = car.distance_from_obstacle() distance_to_obstacle = min(min(dis1, dis2), dis) #选区最小值 print("distance", distance_to_obstacle) if (0 < distance_to_obstacle and distance_to_obstacle < 30): #检查范围,满足条件说明,车找到杯子了 print("find cup") return
def demo_move_step_find_object(ip, speed=20, dis=1, object='cup', vAngle=40, hAngle=80): """ 连续移动寻找物体 Parameter ------- *ip:string -树莓派的Ip *object:string -要寻找的物体 *vAngle:int -垂直方向的角度 *hAngle:int -水平方向的角度 """ car = Car(ip) camera = Camera() camera.connect_server(ip) camera.start_receive() camera.thread_play() ai = Ai(classes="./aiLib/coco/coco.names", config="./aiLib/coco/yolov3.cfg", weight="./aiLib/coco/yolov3.weights") car.turn_servo_camera_vertical(vAngle) car.turn_servo_camera_horizental(hAngle) mainThread_ = threading.Thread(target=find_object, args=( camera, ai, object, )) mainThread_.start() Cruising(car, 4) # 以4的速度进行漫游 car.turn_servo_camera_vertical(vAngle) car.turn_servo_camera_horizental(hAngle) global STOP_FLAGE while CAMERA_FLAGE: if STOP_FLAGE == True: car.stop_all_wheels() print('over .............................................') return status = get_status_with_camera(car, camera, ai, object) car.turn_servo_camera_horizental(90) print(status) if status == 'status_move': car.run_forward(speed, dis) if status == 'status_turn_right': car.spin_right(10, 0.4) if status == 'status_turn_left': car.spin_left(10, 0.4) if status == 'status_stop': return