Created
November 26, 2018 13:10
-
-
Save DataTurks/0bc784d7483ae0248fdcf0049a043fb4 to your computer and use it in GitHub Desktop.
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
# coding: utf-8 | |
import json | |
import matplotlib.pyplot as plt | |
from PIL import Image, ImageDraw | |
import numpy as np | |
import cv2 | |
# Find OpenCV version | |
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') | |
def getFPS(video): | |
major_version = major_ver; | |
if not major_version: | |
(major_version, _, _) = (cv2.__version__).split('.') | |
if int(major_version) < 3 : | |
fps = video.get(cv2.cv.CV_CAP_PROP_FPS) | |
else : | |
fps = video.get(cv2.CAP_PROP_FPS) | |
return fps | |
def getFrameAtTime(video, timeInSec, fps): | |
frameNumber = int(timeInSec* fps); | |
video.set(cv2.CAP_PROP_POS_FRAMES, frameNumber-1) | |
success, frame = video.read() | |
if not success: | |
print ("Failed to get image from frame number " + str(frameNumber)); | |
return frame; | |
colors=[(255,0,0,120), (0,255,0,120), (0,0,255,120), (255,255,0,120), | |
(0,255,255,120), (255,0,255,120), (128,128,0,120), (155,0,155,120), (155,155,20,120)] | |
videoPath = '/Users/mohan/Downloads/cars5.mp4' | |
outpath = "/Users/mohan/Downloads/test/" | |
dataturks_JSON_FilePath = "/Users/mohan/Downloads/Demo_Video_Bounding_Box.json" | |
lines = [] | |
with open(dataturks_JSON_FilePath, 'r') as f: | |
lines = f.readlines() | |
data = json.loads(lines[0]) #just showing this for one video. | |
# read the video from local filesystem. | |
vidcap = cv2.VideoCapture(videoPath) | |
fps = getFPS(vidcap) | |
columns = 3 | |
objNumber = 0 | |
# for each object in the video. | |
for obj in data["annotation"]: | |
objNumber +=1 | |
labelName = obj["label"] | |
print("Object #" + str(objNumber) + ": " + labelName) | |
boundingColor = colors[objNumber%len(colors)] | |
figure=plt.figure(figsize=(15,10)) | |
# display all the positions of the object in the video. | |
positionNumber = 0; | |
for pos in obj["positions"]: | |
time = pos["time"] #the time of bounding box. | |
points = pos["points"] | |
frame = getFrameAtTime(vidcap, time, fps); #extract the frame at the given time,\. | |
#save the frame as an image. | |
num = int(time*fps) | |
outImgPath = outpath + "cars_image%d.jpg" % num | |
cv2.imwrite(outImgPath, frame) | |
#draw a bounding box on the frame image. | |
im = Image.open(outImgPath) | |
width, height = im.size | |
draw = ImageDraw.Draw(im, 'RGBA') | |
#draw the bounding box. | |
draw.rectangle(((points[0][0]*width,points[0][1]*height), (points[3][0]*width,points[3][1]*height)), boundingColor) | |
#display the image in columns. | |
figure.add_subplot(len(obj["positions"])/columns + 1, columns, positionNumber + 1) | |
plt.imshow(np.asarray(im)) | |
positionNumber+=1 | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment