def show_recognize(clf, img, rects): red = [0,0,0xff] if len(rects) == 0: return for i,rect in enumerate(rects): img2 = op.get_subimage(img, rect[0], rect[1]) cv2.imshow(str(i),img2) res = model.recognize_number(clf, img2) cv2.putText(img, str(int(res)), rect[0], cv2.FONT_HERSHEY_SIMPLEX,0.8, red, thickness = 2)
def show_recognize(clf, img, rects): red = [0, 0, 0xff] if len(rects) == 0: return for i, rect in enumerate(rects): img2 = op.get_subimage(img, rect[0], rect[1]) cv2.imshow(str(i), img2) res = model.recognize_number(clf, img2) cv2.putText(img, str(int(res)), rect[0], cv2.FONT_HERSHEY_SIMPLEX, 0.8, red, thickness=2)
def on_mouse(event, x, y, flags, param): global point_s global img global rects green = [0x0, 0xFF, 0x0] read = [0x0, 0x0, 0xFF] if event == cv2.cv.CV_EVENT_LBUTTONDOWN: img_cpy = img.copy() # copy image to draw rectangle point_s = (x, y) cv2.circle(img_cpy, point_s, 1, green) cv2.imshow(win_name, img_cpy) elif event == cv2.cv.CV_EVENT_RBUTTONDOWN: print 'cancel last region' if len(rects) != 0: rects.pop() target_imgs.pop() elif event == cv2.cv.CV_EVENT_MOUSEMOVE and ( flags & cv2.cv.CV_EVENT_FLAG_LBUTTON): img_cpy = img.copy() # copy image to draw rectangle point_e = (x, y) if point_s != point_e: cv2.circle(img_cpy, point_e, 2, green) # draw point cv2.rectangle(img_cpy, point_s, point_e, read) cv2.imshow(win_name, img_cpy) elif event == cv2.cv.CV_EVENT_LBUTTONUP: point_e = (x, y) img_cpy = img.copy() # copy image to draw rectangle if point_s != point_e: cv2.circle(img_cpy, point_e, 2, green) cv2.rectangle(img_cpy, point_s, point_e, read) cv2.imshow(win_name, img_cpy) img2 = op.get_subimage(img, point_s, point_e) # img2 = cv2.resize(img2,(26,26)) # cv2.imwrite('heihei.jpg',img2) rects.append((point_s, point_e)) target_imgs.append(img2) print 'add region', point_s, point_e
def on_mouse(event, x, y, flags, param): global point_s global img global rects green = [0x0,0xFF,0x0] read = [0x0,0x0,0xFF] if event == cv2.cv.CV_EVENT_LBUTTONDOWN: img_cpy = img.copy() # copy image to draw rectangle point_s = (x,y) cv2.circle(img_cpy, point_s, 1, green) cv2.imshow(win_name,img_cpy) elif event == cv2.cv.CV_EVENT_RBUTTONDOWN: print 'cancel last region' if len(rects) != 0: rects.pop() target_imgs.pop() elif event == cv2.cv.CV_EVENT_MOUSEMOVE and (flags & cv2.cv.CV_EVENT_FLAG_LBUTTON): img_cpy = img.copy() # copy image to draw rectangle point_e = (x,y) if point_s != point_e: cv2.circle(img_cpy, point_e, 2, green) # draw point cv2.rectangle(img_cpy, point_s, point_e, read) cv2.imshow(win_name,img_cpy) elif event == cv2.cv.CV_EVENT_LBUTTONUP: point_e = (x,y) img_cpy = img.copy() # copy image to draw rectangle if point_s != point_e: cv2.circle(img_cpy, point_e, 2, green) cv2.rectangle(img_cpy, point_s, point_e, read) cv2.imshow(win_name,img_cpy) img2 = op.get_subimage(img, point_s, point_e) # img2 = cv2.resize(img2,(26,26)) # cv2.imwrite('heihei.jpg',img2) rects.append((point_s,point_e)) target_imgs.append(img2) print 'add region', point_s, point_e
def get_score_info(video): global step score_frames = [] twenty_four_frames = [] camera_change_frames = [] score_a, score_b = 0, 0 # score of both team last_sec = 0 # 24 second board # last_frame = None pos = 0 # frame position video.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos) clf = model.load_classifier() while (video.isOpened()): if pos % step == 0: ret, frame = video.red() else: ret = video.grab() if not ret: break # skip each step steps if pos % step != 0: pos += 1 continue # if camera change if pos != 0 and hist.camera_change(last_frame, frame): camera_change_frames.append(pos) print 'camera change', pos last_frame = frame if op.has_score_board(frame, target_imgs[0], rects[0]): # score a img = op.get_subimage(frame, rects[1][0], rects[1][1]) num = model.recognize_number(clf, img) # print 'a ', num if score_two_or_three(num, score_a): score_frames.append(pos) print 'a from', score_a, 'to', num, pos if not noise(num, score_a): score_a = num # score b img = op.get_subimage(frame, rects[2][0], rects[2][1]) num = model.recognize_number(clf, img) # print 'b ', num if score_two_or_three(num, score_b): score_frames.append(pos) print 'b from', score_b, 'to', num, pos if not noise(num, score_b): score_b = num # 24 second board img = op.get_subimage(frame, rects[3][0], rects[3][1]) sec = model.recognize_number(clf, img) if last_sec != 24 and sec == 24: print 'twenty four', pos twenty_four_frames.append(pos) last_sec = sec pos += 1 # print pos print score_frames, twenty_four_frames, camera_change_frames return score_frames, twenty_four_frames, camera_change_frames
def get_score_info(video): global step score_frames = [] twenty_four_frames = [] camera_change_frames = [] score_a, score_b = 0,0 # score of both team last_sec = 0 # 24 second board # last_frame = None pos = 0 # frame position video.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos) clf = model.load_classifier() while(video.isOpened()): if pos % step == 0: ret, frame = video.red() else: ret = video.grab() if not ret: break # skip each step steps if pos % step != 0: pos += 1 continue # if camera change if pos != 0 and hist.camera_change(last_frame, frame): camera_change_frames.append(pos) print 'camera change', pos last_frame = frame if op.has_score_board(frame, target_imgs[0], rects[0]): # score a img = op.get_subimage(frame, rects[1][0], rects[1][1]) num = model.recognize_number(clf, img) # print 'a ', num if score_two_or_three(num,score_a): score_frames.append(pos) print 'a from',score_a,'to',num, pos if not noise(num, score_a): score_a = num # score b img = op.get_subimage(frame, rects[2][0], rects[2][1]) num = model.recognize_number(clf, img) # print 'b ', num if score_two_or_three(num,score_b): score_frames.append(pos) print 'b from',score_b,'to', num, pos if not noise(num, score_b): score_b = num # 24 second board img = op.get_subimage(frame, rects[3][0], rects[3][1]) sec = model.recognize_number(clf, img) if last_sec != 24 and sec == 24: print 'twenty four',pos twenty_four_frames.append(pos) last_sec = sec pos += 1 # print pos print score_frames, twenty_four_frames, camera_change_frames return score_frames,twenty_four_frames,camera_change_frames