|
| 1 | +''' |
| 2 | +USAGE |
| 3 | +python VideoMarker.py FolderWithImages OutputFileName |
| 4 | +
|
| 5 | +NOTE : LEFT CLICK for one end and RIGHT CLICK for the other |
| 6 | +Keep pressing ENTER to continue. If you want to stop at the middle press 'q' |
| 7 | +
|
| 8 | +LOADING |
| 9 | +numpy.load(OutputFileName.npy) |
| 10 | +
|
| 11 | +''' |
| 12 | + |
| 13 | + |
| 14 | +import argparse |
| 15 | +import sys |
| 16 | +import os |
| 17 | +import cv2 |
| 18 | +import numpy as np |
| 19 | + |
| 20 | + |
| 21 | +def mouse_callback(event, x, y, flags, param): |
| 22 | + global frame |
| 23 | + if event == cv2.EVENT_RBUTTONDOWN: |
| 24 | + points[2], points[3] = x, y |
| 25 | + frame1 = np.array(frame, copy=True) |
| 26 | + cv2.rectangle(frame1,(points[0],points[1]),(points[2],points[3]),(0,255,0),3) |
| 27 | + cv2.imshow('frame', frame1) |
| 28 | + # print(x, y, points) |
| 29 | + if event == cv2.EVENT_LBUTTONDOWN: |
| 30 | + points[0], points[1] = x, y |
| 31 | + frame1 = np.array(frame, copy=True) |
| 32 | + cv2.rectangle(frame1,(points[0],points[1]),(points[2],points[3]),(0,255,0),3) |
| 33 | + cv2.imshow('frame', frame1) |
| 34 | + # print(x, y, points) |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + FILE_NAME=sys.argv[1] |
| 40 | + |
| 41 | + cv2.namedWindow('frame') |
| 42 | + cv2.setMouseCallback('frame', mouse_callback) |
| 43 | + |
| 44 | + cap = cv2.VideoCapture(FILE_NAME) |
| 45 | + ans = [] |
| 46 | + points = [0, 0, 0, 0] |
| 47 | + |
| 48 | + while (1): |
| 49 | + ret, frame = cap.read() |
| 50 | + if ret == True: |
| 51 | + |
| 52 | + frame1 = np.array(frame, copy = True) |
| 53 | + cv2.rectangle(frame1, (points[0], points[1]), (points[2], points[3]), (0, 255, 0), 2) |
| 54 | + cv2.imshow('frame', frame1) |
| 55 | + |
| 56 | + k = cv2.waitKey(0) & 0xff |
| 57 | + if k == ord('q'): |
| 58 | + break |
| 59 | + |
| 60 | + print (points) |
| 61 | + x1, y1, x2, y2 = points |
| 62 | + points[0] = min(x1, x2) |
| 63 | + points[1] = min(y1, y2) |
| 64 | + points[2] = max(x1, x2) |
| 65 | + points[3] = max(y1, y2) |
| 66 | + |
| 67 | + ans.append(points) |
| 68 | + |
| 69 | + |
| 70 | + print("Saved") |
| 71 | + np.save(sys.argv[2], ans) |
0 commit comments