Movie-Barcode-Generator/Barcode_Generator.py

42 lines
853 B
Python
Raw Normal View History

2019-09-26 14:49:22 +00:00
import sys
import cv2
import numpy as np
from PIL import Image
2019-09-26 14:49:22 +00:00
from multiprocessing import Pool
import os
2019-09-26 14:49:22 +00:00
os.system("taskset -p 0xff %d" % os.getpid())
def frame_avg(img):
scaled = img.astype('uint32')
squared = scaled**2
2019-09-26 14:49:22 +00:00
avgsq = np.average(squared, axis=1)
return np.sqrt(avgsq).astype('uint8')
2019-09-26 15:30:46 +00:00
2019-09-26 14:49:22 +00:00
def movie_iter(movie_name, frames_to_skip):
2019-09-26 15:30:46 +00:00
movie = cv2.VideoCapture(f"{movie_name}.mp4")
2019-09-26 14:49:22 +00:00
s, f = movie.read()
while s:
2019-09-26 14:49:22 +00:00
yield f
for i in range(frames_to_skip):
movie.read()
2019-09-26 15:30:46 +00:00
s, f = movie.read()
2019-09-26 14:49:22 +00:00
2019-09-26 14:49:22 +00:00
def elab(movie_it):
with Pool(8) as p:
res = p.map(frame_avg, movie_it, chunksize=100)
return res
2019-09-26 15:30:46 +00:00
it = movie_iter("movies/SampleVideo_1280x720_1mb.mp4", 4)
2019-09-26 15:30:46 +00:00
res = elab(it)
c = np.array(res)
cc = c.swapaxes(0, 1)
i = Image.fromarray(cc, mode='RGB')
2019-09-26 15:30:46 +00:00
i.save("prova_parall.jpg")