Last active
November 2, 2019 19:14
-
-
Save amitbd1508/e04bcc5ea0f39b656a05bd98d3ffa502 to your computer and use it in GitHub Desktop.
Extract image from video for image labeling/annotating (video-to-image.py) . || Visualize and verify label (visualize-and-verify-label.py)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import argparse | |
| def Convert_(current, dest_): | |
| cap = cv2.VideoCapture(current) | |
| counter=0 | |
| while(True): | |
| # Capture frame-by-frame | |
| ret, frame = cap.read() | |
| if frame is not None: | |
| if counter%30==0: | |
| im = cv2.resize(frame, (640, 360)) | |
| cv2.imwrite(dest_+str(counter)+'.jpg', frame) | |
| print ('saved image no: ', counter) | |
| counter += 1 | |
| else: break | |
| # When everything done, release the capture | |
| cap.release() | |
| cv2.destroyAllWindows() | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--src', required=False, dest='src', type=str, | |
| default='example.mp4', help='Full path of video') | |
| parser.add_argument('--dest', required=False, dest='dest', type=str, | |
| default='d_34635/', help='Full path of image dir') | |
| args = parser.parse_args() | |
| Convert_(args.src, args.dest) | |
| cv2.destroyAllWindows() | |
| #To run the program | |
| #python vid_to_img.py --src video.mp4 --dest video_folder\ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import cv2 | |
| import glob | |
| import numpy as np | |
| import xml.etree.ElementTree as ET | |
| def handle_bad_corners(left, right, top, bottom, im_w, im_h): | |
| left = np.maximum(5, left) | |
| top = np.maximum(5, top) | |
| right = np.minimum(im_w, right) | |
| bottom = np.minimum(im_h, bottom) | |
| return (left, right, top, bottom) | |
| def draw_boxes_and_labels(img, obj_, text_label, box_color=(0, 255, 255)): | |
| img_h, img_w = img.shape[:2] | |
| font = cv2.FONT_HERSHEY_SIMPLEX | |
| font_size = 0.5 | |
| font_color = (0, 0, 0) | |
| # Draw the object boxes | |
| left, right, top, bottom = handle_bad_corners(obj_[0], obj_[1], obj_[2], obj_[3], img_w, img_h) | |
| cv2.rectangle(img, (left, top), (right, bottom), box_color, 4) | |
| # Draw a filled boxes on top of the bounding box (as the background for the labels) | |
| left1, top1, right1, _ = handle_bad_corners(left-2, top-20, right+2, bottom, img_w, img_h) | |
| cv2.rectangle(img, (left1, top1), (right1, top), box_color, -1, 1) | |
| top2 = 10 if top<25 else top-20 | |
| cv2.putText(img, text_label, (left, top), font, font_size, font_color, 1, cv2.LINE_AA) | |
| return img | |
| def viz_xml_annotation(path): | |
| for xml_file in glob.glob(path + '/*.xml'): | |
| tree = ET.parse(xml_file) | |
| root = tree.getroot() | |
| prev_fname = "" | |
| for member in root.findall('object'): | |
| fname_ = root.find('filename').text | |
| if (fname_!=prev_fname): | |
| prev_fname = fname_ | |
| class_ = member[0].text | |
| xmin, ymin = (int(member[4][0].text), int(member[4][1].text)) | |
| xmax, ymax = (int(member[4][2].text), int(member[4][3].text)) | |
| print (fname_, class_) | |
| img = cv2.imread(os.path.join(path, fname_)) | |
| h = img.shape[0] | |
| w = img.shape[1] | |
| crop = 4.0 | |
| img = cv2.resize(img, (int(w/crop), int(h/crop))) | |
| cv2.putText(img, fname_, (0+10, int(h/crop)-10), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), lineType=cv2.LINE_AA) | |
| localized_obj = (int(xmin/crop), int(xmax/crop), int(ymin/crop), int(ymax/crop)) | |
| img = draw_boxes_and_labels (img, localized_obj, class_) | |
| cv2.imshow("output", img) | |
| cv2.waitKey(1000) | |
| image_path = "d_003243(37740-38760_35)/" | |
| viz_xml_annotation(image_path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment