def show_image(img, bb, classes, scores, thresh=.4): """ This function display the image img: the image bb: the bounding boxes classes: the class predictions scores: the confidence scores thresh: the threshold to filter out bounding boxes """ h, w = img.shape[:2] # we do not want to bounding boxes on to of each other, since this doesn't look nice mask = non_maximum_supression(bb, scores) bxs = bb[mask] clss = classes[mask] scr = scores[mask] # draw the bounding boxes on to the image for j in range(len(bxs)): if scr[j] < thresh: continue x1, y1 = int(bxs[j, 1] * h), int(bxs[j, 0] * h) x2, y2 = int(bxs[j, 3] * h), int(bxs[j, 2] * h) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.addText( img, '%s: %.0f%%' % (category_names[int(clss[j])], scr[j] * 100), (x1, y1), 'Consolas', 14) # show the image with the bounding boxes cv2.imshow(window_name, img) return img
def _debug_display(self, frame, white, brown, shown_frames): if cv2.waitKey(1) & 0xFF == ord('w'): LOG.debug("White") white = True elif cv2.waitKey(1) & 0xFF == ord('b'): LOG.debug('Brown') brown = True if white and brown: LOG.warning("White and Brown are both set, unknown interactions!") if white: shown_frames += 1 cv2.addText(frame, "White Glass", (10, 20), 'Times') if shown_frames == SHOW_FRAMES: shown_frames = 0 white = False if brown: shown_frames += 1 cv2.addText(frame, "Brown Glass", (10, 20), "Times") if shown_frames == SHOW_FRAMES: shown_frames = 0 brown = False return frame, white, brown, shown_frames
def check(dataset): for i, (x, y) in enumerate(dataset): x = x.numpy().reshape(256, 256, 3) y = '\n'.join(str(y)) cv2.addText(img=x, text=y, nameFont=cv2.FONT_HERSHEY_COMPLEX, org=(10, 10)) cv2.imshow('frame', x) cv2.waitKey(30)
def detect(gray, frame): font = cv2.FONT_HERSHEY_COMPLEX faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), ((x + w), (y + h)), (255, 0, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = frame[y:y + h, x:x + w] smiles = smile_cascade.detectMultiScale(roi_gray, 1.8, 20) for (sx, sy, sw, sh) in smiles: cv2.rectangle(roi_color, (sx, sy), ((sx + sw), (sy + sh)), (0, 0, 255), 2) cv2.addText(frame, 'Face', (x + w, y + h), font, 1, (250, 250, 250), 2, cv2.LINE_AA) return frame
def info_window(self): """returns an image of the info window for this Token""" # this does not work until now. Some problems with the font rendering # @Macky given that we need this at other places as well (menu etc.) there should probably be some functions # dedicated to rendering fonts in proper sizes onto images (resizing new lines etc.) img = np.ones((50, 50, 3)).astype(np.uint8) * 100 row = 10 for type, d in self.descriptors.items(): # cv.addText(img, type + ": " + d, (row, 10), cv.FONT_HERSHEY_SIMPLEX, 5) cv.addText(img, "test", (row, 10), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) row += 50 return img
def detector_loop(capture, fdb, types, type_imgs): NSKIP = 2 cmap = plt.get_cmap('Dark2') cv2.namedWindow('capture', 0) i = 0 while True: # Capture frame = capture_image(capture) ratio = 320. / frame.shape[0] gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) display_frame = cv2.cvtColor(gray_frame, cv2.COLOR_GRAY2BGR) # Detect if i % NSKIP == 0: faces = get_faces(frame, ratio) face_ids = [] for _, _, encoding in faces: fi, fprob = fdb(encoding) face_ids.append((fi, fprob)) for (box, face_img, encoding), (face_id, face_prob) in zip(faces, face_ids): top, right, bottom, left = box color = np.array(cmap(face_id % 8)) * 255 color = color[[2, 1, 0, 3]] # Transform to OpenCV BGRA color[3] = 0. # 0 = fully opaque in OpenCV cv2.rectangle(display_frame, (left, top), (right, bottom), color, 5) label = types['type'][np.where(face_prob <= types['prob'])[0][0]] cv2.addText(display_frame, f'ID: {face_id} {label} ({face_prob:.2f})', (left, top - 12), 'monospace', color=color, weight=75) overlay(types_imgs[label], display_frame, box) # Display cv2.imshow('capture', display_frame) # Events and refresh cv2.waitKey(10) if cv2.getWindowProperty('capture', cv2.WND_PROP_VISIBLE) < 1: break
def draw_rect(image, boxes, class_names=None, color=None): """ :param image: :param boxes: :param class_names: :param color: :return: """ im = image.copy() boxes = boxes[:, :4].astype(np.int) if not color: color = random_color() for i in range(len(boxes)): y1, x1, y2, x2 = boxes[i] im = cv2.rectangle(im, (x1, y1), (x2, y2), color, int(max(im.shape[:2]) / 200)) if class_names is not None: im = cv2.addText(im, class_names[i], (x1, y1 + 8), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255)) return im
condition.notify() t = time() frame_to_show = np.copy(frame) frame_to_show = cv.rectangle(frame_to_show, hand_position[0], hand_position[1], (0, 255, 0), 5) frame_to_show = finder.place_marker(frame_to_show, hand_position[0], (0, 255, 0)) frame_to_show = finder.place_marker(frame_to_show, hand_position[1], (0, 255, 0)) frame_to_show = finder.place_marker(frame_to_show) cv.displayStatusBar('frame', "to get help press 'h'") text_position = (hand_position[0][0] + 20, hand_position[0][1] - 20) cv.addText(frame_to_show, classes[prediction], text_position, nameFont="Times", pointSize=30, color=(0, 255, 255)) loadingcat[prediction] = 1 show_cats(loadingcat) if debug: cv.imshow("masks_merged", masks_merged) cv.imshow("skin_mask", finder.skin_mask) cv.imshow("foreground_mask", finder.foreground_mask) if hand_position[0] != hand_position[1]: cv.imshow("cut_merged", cut_merged) cv.imshow("frame", frame_to_show) cam.release()
def update(self, blank_image=None, draw_number=False): success, self.camImg = cap.read() self.camImg = cv2.flip(self.camImg, 1) self.camImg = detector.findHands(self.camImg, draw=False) self.lmList, _ = detector.findPosition(self.camImg) if len(self.lmList) != 0: i = 0 if blank_image is not None: for point in self.lmList: cv2.circle(blank_image, point, radius=3, color=(0, 0, 255), thickness=-1) cv2.addText(blank_image, str(i), point, 'Roboto-Regular.ttf', color=(0, 255, 0, 1)) i += 1 a = vector_len(self.lmList[8], self.lmList[12]) b = vector_len(self.lmList[0], self.lmList[5]) d = vector_len(self.lmList[0], self.lmList[8]) c = a / b e = vector_len(self.lmList[3], self.lmList[5]) c2 = e / b # self.isDown = c < .35 and d > b self.isDown = c2 < .38 if blank_image is not None and draw_number: cv2.addText(blank_image, '8-12: ' + str(a), (10, 10), 'Roboto-Regular.ttf', color=(255, 0, 0, 1)) cv2.addText(blank_image, '0-5: ' + str(b), (10, 22), 'Roboto-Regular.ttf', color=(255, 0, 0, 1)) cv2.addText(blank_image, '8-12/0-5: ' + str(c), (10, 34), 'Roboto-Regular.ttf', color=(255, 0, 0, 1)) cv2.addText(blank_image, 'isDown: ' + str(self.isDown), (10, 46), 'Roboto-Regular.ttf', color=(255, 0, 0, 1)) cv2.addText(blank_image, '3-5: ' + str(e), (10, 58), 'Roboto-Regular.ttf', color=(255, 0, 0, 1)) cv2.addText(blank_image, '3-5/0-5: ' + str(c2), (10, 70), 'Roboto-Regular.ttf', color=(255, 0, 0, 1)) cv2.addText(blank_image, 'isDown: ' + str(c2 < .38), (10, 82), 'Roboto-Regular.ttf', color=(255, 0, 0, 1)) x = self.lmList[8][0] / W y = self.lmList[8][1] / (H * HK) if x > 1: x = 1 if x < 0: x = 0 if y > 1: y = 1 if y < 0: y = 0 self.position = (x, y)
def main(): global img startUp() cleanImage = False cap = cv2.VideoCapture(videoFileName) fourcc = cv2.VideoWriter_fourcc(*'XVID') [valid, img] = cap.read() dotsL = np.array((img.shape[0], 2), dtype=np.uint8) imgMod = warped(doImageProcess(img)) #left_fit, right_fit,,, = curveStepOne(imgMod) #cv2.imshow("Test", imgMod) cv2.imshow("Mix", img) cv2.setMouseCallback('Mix', takeBrick) font = 'FONT_HERSHEY_SIMPLEX' frame = 0 while valid: frame += 1 if frame == 2: pass baseTime = time() # undistort the image using the matrix from calibration img = cv2.undistort(img, mtx, dist, None, mtx) print("Undistort {:3.2f}".format(1000 * (time() - baseTime))) imgMod = warped(img) print("Filters {:3.2f}".format(1000 * (time() - baseTime))) imgMod = doImageProcess(imgMod) print("warped {:3.2f}".format(1000 * (time() - baseTime))) polW = np.zeros(img.shape, dtype='uint8') polWP = np.zeros(img.shape, dtype='uint8') dst = np.zeros(img.shape) left_fit, right_fit, laneWidth, lwf, rwf, out_img = curveStepOne( imgMod) print("Curve {:3.2f}".format(1000 * (time() - baseTime))) left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2] right_fitx = right_fit[0] * ploty**2 + right_fit[ 1] * ploty + right_fit[2] dotsL = np.dstack((left_fitx, ploty)) dotsR = np.dstack((right_fitx, ploty)) par_fit = gen_parallel(left_fit, 3.7 / xprop, 0, 720) parx_fit = fquad(par_fit, ploty) dotsPar = np.dstack((parx_fit, ploty)) poligon = np.concatenate( (np.int32(dotsL), np.flip(np.int32(dotsR), axis=1)), axis=1) print("Prepare polys {:3.2f}".format(1000 * (time() - baseTime))) if lwf: lColor = (255, 0, 255) else: lColor = (0, 0, 255) if rwf: rColor = (255, 0, 255) else: rColor = (0, 0, 255) if rwf and lwf: mColor = (0, 255, 0) elif rwf or lwf: mColor = (0, 255, 255) else: mColor = (0, 0, 255) #cv2.fillPoly(polW, [dstPers.astype(int)], (0, 0, 255)) #original calibration polygon cv2.fillPoly(polWP, np.int32(poligon), mColor) cv2.polylines(polWP, np.int32(dotsL), False, lColor, thickness=10) cv2.polylines(polWP, np.int32(dotsR), False, rColor, thickness=10) polT = cv2.perspectiveTransform(poligon.astype(float), Minv) cv2.fillPoly(polW, polT.astype(int), mColor) polT = cv2.perspectiveTransform(dotsL.astype(float), Minv) cv2.polylines(polW, polT.astype(int), False, lColor, thickness=10) polT = cv2.perspectiveTransform(dotsR, Minv) cv2.polylines(polW, polT.astype(int), False, rColor, thickness=10) #cv2.polylines(polW, np.int32(dotsPar), False, (0,0, 255), thickness=10) '''persp3c = np.zeros(img.shape) persp3c[:,:,0]= 0 persp3c[:,:,1] = imgMod * 255 persp3c[:,:,2] = imgMod * 255''' #dw = np.uint8(dewarped(polW)) print("dewarped {:3.2f}".format(1000 * (time() - baseTime))) dst = cv2.addWeighted(img, .7, np.uint8(polW), .3, 0.0) print("Weighted 1 {:3.2f}".format(1000 * (time() - baseTime))) cv2.addText(dst, "Width: {:.2} Frame: {:}".format(laneWidth, frame), (10, 30), font, 15, (200, 0, 0)) cv2.addText(dst, "Left ok: {:} Right ok: {:}".format(lwf, rwf), (10, 60), font, 15, (200, 0, 0)) print("Text 1 {:3.2f}".format(1000 * (time() - baseTime))) lc, rc = curvature(left_fitx, right_fitx, ploty, 700) mlc = curvatureLine(left_fit, ploty)[700] mrc = curvatureLine(right_fit, ploty)[700] mpc = curvatureLine(par_fit, ploty)[700] cv2.addText( dst, "Left curvature: {:.0f} Right curvature: {:.0f}".format(lc, rc), (10, 90), font, 15, (255, 0, 255)) center_distance = getOffsetFromCenter(left_fit, right_fit) cv2.addText(dst, "Distance to lane center: {:.2f}".format(center_distance), (10, 120), font, 15, (255, 0, 255)) print("Curvature 1 {:3.2f}".format(1000 * (time() - baseTime))) '''for i in range(1): lc, rc = curvature(left_fitx,right_fitx,ploty,720-40-i*80) cv2.addText(dst, "Pos y {} Left curvature: {:.0f} Right curvature: {:.0f}".format(720-40-i*80,lc, rc), (10, 60+20*i), font, 15, (255, 0, 255)) print("Text 2 {:3.2f}".format(1000 * (time() - baseTime)))''' #cv2.imshow("Org", img) #cv2.imshow("Persp", out_img) out2 = cv2.resize(polWP, (320, 180)) out3 = cv2.resize(out_img, (320, 180)) dst[0:180, 960:] = out2 dst[185:365, 960:] = out3 if cleanImage: cv2.imshow("Mix", img) else: cv2.imshow("Mix", dst) cv2.imwrite( videoFileName[:-4] + "/" + videoFileName[:-4] + '_{:04}'.format(frame) + '.jpg', dst) if (laneWidth > 4): pass k = chr(cv2.waitKey(5) & 255) print("Draw {:3.2f}".format(1000 * (time() - baseTime))) if k == 'p': k = cv2.waitKey(100) while k != 'c' and k != 'p': k = chr(cv2.waitKey(100) & 255) pass if k == 't': #Enter in training mode cleanImage = not cleanImage if k == 'm': frame = 547 cap.set(cv2.CAP_PROP_POS_FRAMES, frame) if k == 'f': frame += 60 cap.set(cv2.CAP_PROP_POS_FRAMES, frame) if k == 'r': if frame > 60: frame -= 60 else: frame = 0 cap.set(cv2.CAP_PROP_POS_FRAMES, frame) [valid, img] = cap.read()
def do_thang(last_notes): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.bitwise_not(gray) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] top_finished_streaks = [] streak = [] for a in range(len(thresh[10])): if thresh[10][a] == 0: streak.append(a) if thresh[10][a] == 255 and len(streak) > 0: if len(streak) < 5: streak = [] else: top_finished_streaks.append(streak) streak = [] streak = [] bottom_finished_streaks = [] for a in range(len(thresh[478])): if thresh[478][a] == 0: streak.append(a) if thresh[478][a] == 255 and len(streak) > 0: if len(streak) < 5: streak = [] else: bottom_finished_streaks.append(streak) streak = [] print(len(top_finished_streaks), len(bottom_finished_streaks)) print(top_finished_streaks[0][0], 10, top_finished_streaks[-1][-1], 10, bottom_finished_streaks[0][0], 478, bottom_finished_streaks[-1][-1], 478) top_l = (top_finished_streaks[0][0], 10) top_r = (top_finished_streaks[-1][-1], 10) bot_l = (bottom_finished_streaks[0][0], 478) bot_r = (bottom_finished_streaks[-1][-1], 478) coords = [top_l, top_r, bot_l, bot_r] top_m = (((top_r[0] - top_l[0]) / 2) + top_l[0], 10) bot_m = (((bot_r[0] - bot_l[0]) / 2) + bot_l[0], 478) top_m_i = (int(top_m[0]), int(top_m[1])) bot_m_i = (int(bot_m[0]), int(bot_m[1])) print("midpt", top_m, bot_m, (bot_m[0] - top_m[0])) cv2.line(frame, top_m_i, bot_m_i, (0, 0, 255), thickness=1) if bot_m[0] != top_m[0]: ang = math.atan(478 / (bot_m[0] - top_m[0])) print("nang", math.degrees(ang)) top_mid_dis_a = (top_r[0] - top_l[0]) / 2 hyp = top_mid_dis_a * math.sin(ang) * 2 side_ang = math.pi / 2 - ang xer = hyp * math.cos(side_ang) yer = hyp * math.sin(side_ang) print("nadj", xer, yer, side_ang) print("nhyp", hyp, top_mid_dis_a) if ang > 0: top_mod = (int(top_r[0] - xer), int(top_r[1] + yer)) p = (top_mod, top_r) elif ang < 0: top_mod = (int(top_l[0] + xer), int(top_l[1] - yer)) p = (top_l, top_mod) xstep = abs(xer / hyp) ystep = - (yer / hyp) xc = p[0][0] yc = min(p[0][1], p[1][1]) liner = [] while xc < p[1][0]: xc = xc + xstep yc = yc + ystep liner.append(thresh[int(yc)][int(xc)]) paper_width = 70 xstep = abs(xer / paper_width) ystep = - yer / paper_width xc = p[0][0] yc = p[0][1] new_notes = [] for bb in range(30): ccy = int(yc + (6.3 * ystep) + (bb * ystep * 2)) ccx = int(xc + (6.3 * xstep) + (bb * xstep * 2)) new_notes.append(thresh[ccy][ccx]) if thresh[ccy][ccx] == 255: thickness = -1 else: thickness = 1 cv2.circle(frame, (ccx, ccy), int(xstep), (0, 0, 255), thickness=thickness,) note_string = " " for i, pair in enumerate(zip(last_notes, new_notes)): msg = False if pair == (0, 255): print("trigger a ", note_data[i]) msg = mido.Message('note_on', note=note_data[i]) elif pair == (255, 0): print("trigger a ", note_data[i]) msg = mido.Message('note_off', note=note_data[i]) if msg: port.send(msg) if pair[1] == 255: note_string += notes[(60 - note_data[i]) % 12] + ", " cv2.addText(frame, note_string, (30, 400), "Times", 12, (0, 0, 255)) else: p = (top_l, top_r) cv2.line(frame, p[0], p[1], (0, 0, 255), thickness=1) # for pix in range(int(hyp)): for coord in coords: # Display the resulting frame cv2.circle(frame, coord, 10, (255, 255, 0), thickness=1) cv2.imshow('thresh',thresh) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): raise ROOB return new_notes
def display_mask_image(cv_window_name,image, boxes, class_ids, class_names, scores=None, title="", figsize=(16, 16), ax=None, show_mask=True, show_bbox=True, colors=None, captions=None, score_threshold=0.8): """ boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates. masks: [height, width, num_instances] class_ids: [num_instances] class_names: list of class names of the dataset scores: (optional) confidence scores for each box title: (optional) Figure title show_mask, show_bbox: To show masks and bounding boxes or not figsize: (optional) the size of the image colors: (optional) An array or colors to use with each object captions: (optional) A list of strings to use as captions for each object """ # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") return image else: assert boxes.shape[0] == class_ids.shape[0] # If no axis is passed, create one and automatically call show() auto_show = True # if not ax: # _, ax = plt.subplots(1, figsize=figsize) # auto_show = True # # Generate random colors # colors = visualize.fixed_colors(N) colors = visualize.get_colors(N) # Show area outside image boundaries. # height, width = image.shape[:2] # ax.set_ylim(height + 10, -10) # ax.set_xlim(-10, width + 10) # ax.axis('off') # ax.set_title(title) scoreMin=score_threshold masked_image=image.astype(np.uint8).copy() for i in range(N): print('class:',class_names[class_ids[i]],'score:',scores[i]) if scores[i] > scoreMin: # Mask # mask = masks[:, :, i] # color = colors[i] # color = colors[class_ids[i]] color = colors[0] # color = # if show_mask: # masked_image = visualize.apply_mask(image, mask, color,alpha=0.4) masked_image = masked_image.astype(np.uint8).copy() for i in range(N): if scores[i]>scoreMin: # color = colors[i] # color = colors[class_ids[i]] color = colors[0] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: image = cv2.rectangle(masked_image, (x1, y1),(x2, y2),(100,20,100),thickness=2) # p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, # alpha=0.7, linestyle="dashed", # edgecolor=color, facecolor='none') # ax.add_patch(p) # Label if not captions: class_id = class_ids[i] score = scores[i] if scores is not None else None label = class_names[class_id] # x = random.randint(x1, (x1 + x2) // 2) # caption = "{} {:.3f}".format(label, score) if score else label caption = "{} ".format(label) if score else label else: caption = captions[i] image = cv2.addText(image,caption, (x1, y1-1),'Times',pointSize=13,color=(178,34,34)) # ax.text(x1, y1 + 8, caption, # color='w', size=11, backgroundcolor="none") # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. # padded_mask = np.zeros( # (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) # padded_mask[1:-1, 1:-1] = mask # contours = visualize.find_contours(padded_mask, 0.5) # for verts in contours: # # Subtract the padding and flip (y, x) to (x, y) # verts = np.fliplr(verts) - 1 # print('verts:',verts) # points = np.array([[(1,1),(20,20),(15,15)]], dtype=np.int32) # masked_image = cv2.fillPoly(masked_image,points,color) # p = Polygon(verts, facecolor="none", edgecolor=color) # ax.add_patch(p) # ax.imshow(masked_image.astype(np.uint8)) # cv2.imshow(cv_window_name, masked_image) # cv2.waitKey(1) # if auto_show: # plt.show() return masked_image
def main(): """Main function""" parser = argparse.ArgumentParser(description="Analyze Results") parser.add_argument("-p", "--pred_dataset_file", required=True, help="Path to predicted (results) RenderAndCompare JSON dataset file") parser.add_argument("-g", "--gt_dataset_file", required=True, help="Path to groundtruth RenderAndCompare JSON dataset file") args = parser.parse_args() gt_dataset, pred_dataset = load_datasets(args.gt_dataset_file, args.pred_dataset_file) print "gt_dataset = {}".format(gt_dataset) print "pred_dataset = {}".format(pred_dataset) cv2.namedWindow('image', cv2.WINDOW_NORMAL) paused = True fwd = True i = 0 while True: i = max(0, min(i, pred_dataset.num_of_images() - 1)) pred_image_info = pred_dataset.image_infos()[i] gt_image_info = gt_dataset.image_infos()[i] if gt_dataset else None if gt_image_info: assert gt_image_info['image_file'] == pred_image_info['image_file'] assert gt_image_info['image_size'] == pred_image_info['image_size'] assert gt_image_info['image_intrinsic'] == pred_image_info['image_intrinsic'] img_path = osp.join(pred_dataset.rootdir(), pred_image_info['image_file']) assert osp.exists(img_path), 'Image file {} does not exist'.format(img_path) image = cv2.imread(img_path) for j in xrange(len(pred_image_info['object_infos'])): pred_obj = pred_image_info['object_infos'][j] gt_obj = gt_image_info['object_infos'][j] if gt_image_info else None draw_object(image, pred_obj, (0, 0, 255)) obj_text = "{}_{}".format(pred_obj['category'], pred_obj['id']) bbx_visible = np.array(pred_obj['bbx_visible'], dtype=np.float32) tl = tuple(np.floor(bbx_visible[:2]).astype(int)) font_face = cv2.FONT_HERSHEY_PLAIN font_scale = 0.8 thickness = 1 ts, baseline = cv2.getTextSize(obj_text, font_face, font_scale, thickness) cv2.rectangle( image, (tl[0], tl[1] + baseline), (tl[0] + ts[0], tl[1] - ts[1]), (0, 0, 0), cv2.FILLED ) cv2.addText(image, obj_text, tl, 'times', color=(0, 255, 0)) if gt_obj: assert gt_obj['id'] == pred_obj['id'] assert gt_obj['category'] == pred_obj['category'] draw_object(image, gt_obj, (0, 255, 0)) cv2.line(image, tuple(np.array(pred_obj['center_proj'], dtype=np.float32)), tuple(np.array(gt_obj['center_proj'], dtype=np.float32)), (0, 0, 255), 1, cv2.LINE_AA) cv2.displayOverlay('image', 'Image: {}'.format(osp.splitext(osp.basename(img_path))[0])) cv2.imshow('image', image) key = cv2.waitKey(not paused) if key == 27: cv2.destroyAllWindows() break elif key in [82, 83, 100, 119, 61, 43]: fwd = True elif key in [81, 84, 97, 115, 45]: fwd = False elif key == ord('p'): paused = not paused i = i+1 if fwd else i-1
def main(): # setup folder t0 = last = pause_until = time.time() folder = str(t0)[-4:] os.makedirs('data/%s' % folder, exist_ok=True) # Capture the first frame. cap = cv2.VideoCapture(0) ret, frame = cap.read() i = 0 # movement specification speed = 3 dx = dy = 1 padding = 100 padding_warning = 150 min_duration_between_frames = 0.5 # in seconds # movement initialization h, w, _ = frame.shape x = random.randint(padding, w - padding) y = random.randint(padding, h - padding) while True: # Capture frame-by-frame ret, frame = cap.read() frame = cv2.flip(frame, 1) # flip across vertical axis t1 = time.time() # save image with label, once per second if t1 - last > min_duration_between_frames and t1 > pause_until: frame_to_draw = cv2.resize(frame, (w // 8, h // 8)) last = time.time() side = 'right' if x > w // 2 else 'left' info = (folder, side, i, x // 8, y // 8) i += 1 filename = 'data/%s/%s_%06d_x_%d_y_%d.png' % info cv2.imwrite(filename, frame_to_draw) # pause at the center of the image to switch hands if abs(x - w / 2) < 2: x += dx * 6 pause_until = t1 + 3 # move and draw dot if t1 > pause_until: x += dx * speed y += dy * speed cv2.circle(frame, (x, y), 5, (0, 0, 255), thickness=-1) # Warning before changing direction if x <= padding_warning or x >= w - padding_warning or \ y <= padding_warning or y >= h - padding_warning: cv2.imshow('frame', frame) # hack cv2.addText(frame, "changing direction", (x + 50, y), "Calibri", color=RED) # Display the resulting frame cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # bounce dot away from the edge if x <= padding or x >= w - padding: dx *= -1 if y <= padding or y >= h - padding: dy *= -1 # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def main(): parser = argparse.ArgumentParser(description="Visualize Results") parser.add_argument("pred_dataset_file", help="Path to predicted (results) JSON dataset file") parser.add_argument("-s", "--score_threshold", default=0.1, type=float, help="Score thresold") args = parser.parse_args() assert osp.exists(args.pred_dataset_file ), "ImageDataset filepath {} does not exist.".format( args.pred_dataset_file) print 'Loading predited dataset from {}'.format(args.pred_dataset_file) pred_dataset = ImageDataset.from_json(args.pred_dataset_file) print 'Loaded {} dataset with {} annotations'.format( pred_dataset.name(), pred_dataset.num_of_images()) print "score_threshold = {}".format(args.score_threshold) cv2.namedWindow('image', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) cv2.resizeWindow('image', 2048, 1024) wait_nav = WaitKeyNavigator(pred_dataset.num_of_images()) wait_nav.print_key_map() quit_viz = False while not quit_viz: i = wait_nav.index image_info = pred_dataset.image_infos()[i] img_path = osp.join(pred_dataset.rootdir(), image_info['image_file']) image = cv2.imread(img_path) for obj_info in image_info['object_infos']: if 'bbx_visible' in obj_info: if 'score' in obj_info: if obj_info['score'] < args.score_threshold: continue draw_bbx(image, obj_info['bbx_visible']) if 'category' in obj_info: obj_text = obj_info['category'] tl = tuple( np.floor(obj_info['bbx_visible'][:2]).astype(int)) font_face = cv2.FONT_HERSHEY_PLAIN font_scale = 0.8 thickness = 1 ts, baseline = cv2.getTextSize(obj_text, font_face, font_scale, thickness) cv2.rectangle(image, (tl[0], tl[1] + baseline), (tl[0] + ts[0], tl[1] - ts[1]), (0, 0, 0), cv2.FILLED) cv2.addText(image, obj_text, tl, 'times', color=(0, 255, 0)) cv2.displayOverlay( 'image', 'Image: {}'.format(osp.splitext(osp.basename(img_path))[0])) cv2.imshow('image', image) quit_viz = wait_nav.process_key()
def detect_and_color_splash(model, image_path=None, video_path=None, outpath=None): assert image_path or video_path # Image or video? if image_path: # Run model detection and generate the color splash effect print("Running on {}".format(args.image)) # Read image image = skimage.io.imread(args.image) # Detect objects r = model.detect([image], verbose=1)[0] # Color splash splash = color_splash(image, r['masks']) # Save output file_name = "splash_{:%Y%m%dT%H%M%S}.png".format( datetime.datetime.now()) file_name = outpath skimage.io.imsave(file_name, splash) elif video_path: import cv2 # Video capture vcapture = cv2.VideoCapture(video_path) width = int(vcapture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vcapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = vcapture.get(cv2.CAP_PROP_FPS) # Define codec and create video writer file_name = "splash_{:%Y%m%dT%H%M%S}.avi".format( datetime.datetime.now()) vwriter = cv2.VideoWriter(file_name, cv2.VideoWriter_fourcc(*'MJPG'), fps, (width, height)) count = 0 success = True while success: print("frame: ", count) # Read next image success, image = vcapture.read() if success: # OpenCV returns images as BGR, convert to RGB image = image[..., ::-1] # Detect objects r = model.detect([image], verbose=0)[0] # Color splash splash = color_splash(image, r['masks']) # Draw Bboxes for count, box in enumerate(r['rois']): # Shape (y1, x1, y2, x2, class_id) splash = cv2.rectangle(splash, (box[1], box[0]), (box[3], box[2]), (255, 0, 0), 2) splash = cv2.addText(splash, r['class_ids'][count], (box[3], box[2]), 2) # RGB -> BGR to save image to video splash = splash[..., ::-1] # Add image to video writer vwriter.write(splash) count += 1 vwriter.release() print("Saved to ", file_name)