def photo_loop(): for fname in os.listdir('chessboard'): if fname[0] == '.': continue frame = cv2.imread('chessboard/' + fname) frame = cv2.resize(frame, IMAGE_SIZE) # frame = cv2.undistort(frame, mtx, dist, None, newcameramtx) frame = cv2.warpPerspective(frame, perspective_matrix, IMAGE_SIZE) cv2.imshow('image', frame) cv2.waitKey(0)
def process_image(frame): output = { "buildings": [] } shapes = [] frame = cv2.resize(frame, IMAGE_SIZE_FOR_TRACING) if IGNORE_RED: frame = cv2.multiply(frame, unred_mask) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blurred = cv2.blur(gray, (BLUR_RADIUS, BLUR_RADIUS)) edges = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, THRESH_RADIUS, THRESH_CONSTANT) retr_external = True contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:] hierarchy = hierarchy[0] top_level_contour_indices = [i for i in range(len(hierarchy)) if -1 == hierarchy[i][3]] biggest_top_level_contour_index = max(top_level_contour_indices, key=lambda i: cv2.contourArea(contours[i])) if len(top_level_contour_indices) else None shape_contour_indices = [i for i in range(len(hierarchy)) if hierarchy[i][3] == biggest_top_level_contour_index] contours = [contours[i] for i in shape_contour_indices] # print hierarchy # (contour, id, name, descriptor) contours = [c for c in contours if cv2.contourArea(c) > MIN_SHAPE_SIZE] contours = [cv2.approxPolyDP(c, cv2.arcLength(c, True) / CONTOUR_SIMPLIFICATION, True) for c in contours] contours.sort(key=lambda x: cv2.contourArea(x), reverse=True) contours = contours[:min(len(contours), MAX_CONTOURS)] if SHOW_SINGLE_CAPTURE_AND_DUMP: for contour in contours: cv2.drawContours(frame, [contour], -1, (255,0,0)) x,y = contour[0][0] i = len(shapes) cv2.putText(frame, str(i), (x,y), cv2.FONT_HERSHEY_PLAIN, 0.7, (0, 0, 255)) shapes.append(contour) else: global TIMESTAMP TIMESTAMP += 1 for (contour, id, name, descriptor) in matcher.run_frame(contours): cv2.drawContours(frame, [contour], -1, (255,0,0)) x,y = contour[0][0] rotation, flipped, rot_without_flipping = matcher.get_rotation(name, contour) label = "{0}:{1}:{2}".format(id, name, int(rotation / math.pi * 180)) if flipped: label += '(F)' cv2.putText(frame, label, (x,y), cv2.FONT_HERSHEY_PLAIN, 0.7, (0, 0, 255)) if matcher.lifetimes[id] >= MIN_SHAPE_LIFETIME: output['buildings'].append(shape_json(contour, id, name, rotation, flipped, rot_without_flipping)) output['timestamp'] = str(TIMESTAMP) if SHOW_SINGLE_CAPTURE_AND_DUMP: f = open('last_rec.pickle', 'w') f.write(pickle.dumps(shapes)) f.close() return frame, output
def photo_demo(): # Capture frame-by-frame frame = cv2.imread("images/camera-view.png") frame = cv2.resize(frame, IMAGE_SIZE) show(frame, 'image') # frame = cv2.undistort(frame, mtx, dist, None, newcameramtx) frame = cv2.warpPerspective(frame, perspective_matrix, IMAGE_SIZE) frame = cv2.flip(frame, 1) frame = cv2.flip(frame, -1) show(frame, 'transformed') frame, output = process_image(frame) global IMAGE IMAGE = frame global OUTPUT OUTPUT = output if SHOW_SINGLE_CAPTURE_AND_DUMP: cv2.imshow('label', frame) cv2.waitKey(0) # cap.release() cv2.destroyAllWindows()
def video_loop(): cap = cv2.VideoCapture(CAMERA_NUM) while(True): # Capture frame-by-frame if BLANKING: blanking.set_blank(True) wait(0.1) if BLANKING: blanking.set_blank(False) wait(0.1) cap.grab() # blanking.set_blank(False) ret, frame = cap.retrieve() frame = cv2.resize(frame, IMAGE_SIZE) # frame = cv2.undistort(frame, mtx, dist, None, newcameramtx) frame = cv2.warpPerspective(frame, perspective_matrix, IMAGE_SIZE) frame = cv2.flip(frame, 1) frame = cv2.flip(frame, -1) frame, output = process_image(frame) cv2.imshow('image', frame) global IMAGE IMAGE = frame global OUTPUT OUTPUT = output if SHOW_SINGLE_CAPTURE_AND_DUMP: cv2.waitKey(0) break if BLANKING: wait(0.7) else: wait(0.01) cap.release() cv2.destroyAllWindows()