def bypass(path_cap, path_frame): # do not need to get frame by read function. # Directly use global variables global path_frame ###### Prepare ######### avoid_client.init(capL_id=2, capR_id=1) # get direction direction = avoid_client.getOrient() first_dir = direction ''' first direction. important variable''' # 先离开障碍物面前 while direction is not 2: if first_dir is 3: # 左转,前进,右转 mo.turn_left(common_config.SLEEP_TIME) mo.forward(common_config.SLEEP_TIME) mo.turn_right(common_config.SLEEP_TIME) elif first_dir is 4: mo.turn_right(common_config.SLEEP_TIME) mo.forward(common_config.SLEEP_TIME) mo.turn_left(common_config.SLEEP_TIME) direction = avoid_client.getOrient() # 试探性地前进 flag = False while(!util.hasRoad(process_img(path_frame))): #在没找到地上的路之前 if first_dir is 3: mo.turn_right(common_config.SLEEP_TIME) if flag is False direction = avoid_client.getOrient() if direction is not 2: mo.turn_left(common_config.SLEEP_TIME) mo.forward(common_config.SLEEP_TIME) flag = False else: mo.forward(common_config.SLEEP_TIME) flag = True elif first_dir is 4: mo.turn_left(common_config.SLEEP_TIME) if flag is False direction = avoid_client.getOrient() if direction is not 2: mo.turn_right(common_config.SLEEP_TIME) mo.forward(common_config.SLEEP_TIME) flag = False else: mo.forward(common_config.SLEEP_TIME) flag = True # 找到路之后,往回转一下即可 mo.turn_left(common_config.SLEEP_TIME) if first_dir is 3 mo.turn_right(common_config.SLEEP_TIME) if first_dir is 4
import image_preprocess as imgprocess import common as common_config import avoid_client import bypass ## Main function THRESHOLD = 220 # major variables cap = cv2.VideoCapture(0) ret, frame = cap.read() mo = move.Move() net = network.load("../config/result/0316-93%") count = 0 # use for get orient # prepare initiation avoid_client.init(capL_id=2, capR_id=1) ## Get Frame part #Continually updates the frame class GetFrameThread(Thread): def __init__(self): Thread.__init__(self) self.thread_stop = False def run(self): global ret, frame while not self.thread_stop: ret, frame = cap.read() def stop(self): self.thread_stop = True