Skip to content

Instantly share code, notes, and snippets.

@bluec0re
Created July 20, 2015 16:30
Show Gist options
  • Save bluec0re/19f24c8840bea60db558 to your computer and use it in GitHub Desktop.
Save bluec0re/19f24c8840bea60db558 to your computer and use it in GitHub Desktop.
Pixelates areas inside videos with opencv
#!/usr/bin/env python2
# encoding: utf-8
import cv2
import cv
import sys
video = cv2.VideoCapture("input.webm")
fps = video.get(cv.CV_CAP_PROP_FPS) / 2
videoout = cv2.VideoWriter()
size = (int(video.get(cv.CV_CAP_PROP_FRAME_WIDTH)), int(video.get(cv.CV_CAP_PROP_FRAME_HEIGHT)))
fourcc = int(video.get(cv.CV_CAP_PROP_FOURCC))
fourcc = fourcc or cv.CV_FOURCC('V', 'P', '8', '0')
videoout.open("blured.webm", fourcc, 15 or video.get(cv.CV_CAP_PROP_FPS), size)
if not videoout.isOpened():
print "Error write. VideoWrite/ffmpeg support missing?"
exit(1)
blurs = [
# [start_frame, end_frame, (pos_left, pos_top), (pos_right, pos_bottum)]
[15, 65, (1500, 180), (2310, 475)],
[265, 310, (1500, 180), (2800, 1600)],
]
i = 0
while True:
ret, img = video.read()
i += 1
if not ret:
break
sys.stdout.write("\r{}".format(i))
sys.stdout.flush()
for b in blurs:
if b[0] <= i <= b[1]:
roi = img[b[2][1]:b[3][1],b[2][0]:b[3][0]]
roi = cv2.GaussianBlur(roi, (33,33), 3)
#img = roi
img[b[2][1]:b[3][1],b[2][0]:b[3][0]] = roi
# cv2.imshow("preview", img)
videoout.write(img)
# if cv2.waitKey(int(1000/fps)) & 0xff == 27:
# print i
# break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment