Unwrapping someone's face to make wrapping paper
My first step image processing with OpenCV for Python
So this last weekend I was pretty bored at home with nothing to do and no one to climb with. During the last days, I made a Twitter bot and I had quite a lot of fun doing it. After some research of the most useful or popular libraries in Python I came across OpenCV, which seems to be a very powerful library for image processing and many other much more complex things.
I decided to give it a try and remember watching this William Osman's video.
He explained more or less the process to create his code and even made his code public in his blog, well he was using a library called PIL but I wanted to learn to use OpenCV so it served for nothing.
So well here is my code and a brief explanation of what it does. Be gentle, I am a newbie, I do this to amuse myself as I find it quite entertaining and educative.
import cv2 as cv
import numpy as np
video = cv.VideoCapture('C:/Users/Usuario/PycharmProjects/wrapping/video/video.mp4')
success,image = video.read()
count = 0
while success:
cv.imwrite("C:/Users/Usuario/PycharmProjects/wrapping/frame/frame%d.jpg" % count, image) # save frame as JPEG file
success,image = video.read()
count += 1
print('Read Frame #', count)
numero = list(range(0,count))
for i in numero:
lectura = "C:/Users/Usuario/PycharmProjects/wrapping/frame/frame" + str(i) + ".jpg"
img_original = cv.imread(lectura)
dimensions = img_original.shape
[y, h, x ,w ] = [0,int(dimensions[0]),int((int(dimensions[1])/2)-1),1]
img_crop = img_original[y:h,x:x+w]
cv.imwrite("C:/Users/Usuario/PycharmProjects/wrapping/crop/crop%d.jpg" % i,img_crop)
print("Crop frame #", i)
li = []
i = 0
for i in numero:
lectura = "C:/Users/Usuario/PycharmProjects/wrapping/crop/crop" + str(i) + ".jpg"
im1 = cv.imread(lectura)
li.append(im1)
print("Append Frame #", i)
im_h_1 = cv.hconcat(li)
cv.imwrite("C:/Users/Usuario/PycharmProjects/wrapping/nuevo/forward.jpg", im_h_1)
print("Concat Done")
for i in reversed(numero):
lectura = "C:/Users/Usuario/PycharmProjects/wrapping/crop/crop" + str(i) + ".jpg"
im1 = cv.imread(lectura)
li.append(im1)
print("Append Frame #", i)
im_h_2 = cv.hconcat(li)
cv.imwrite("C:/Users/Usuario/PycharmProjects/wrapping/nuevo/backward.jpg", im_h_2)
print("Concat Done")
forward = cv.imread("C:/Users/Usuario/PycharmProjects/wrapping/nuevo/forward.jpg")
backward = cv.imread("C:/Users/Usuario/PycharmProjects/wrapping/nuevo/backward.jpg")
im_final = cv.hconcat([backward,forward])
cv.imwrite("C:/Users/Usuario/PycharmProjects/wrapping/nuevo/final.jpg", im_final)
cv.imshow("C:/Users/Usuario/PycharmProjects/wrapping/nuevo/final.jpg",im_final)
cv.waitKey(0)
cv.destroyAllWindows()
It first imports the two libraries that are needed, OpenCV and numpy which is used to work with ease with matrices and stuff like that. I then read the video which I want to convert and strip each frame of that video. Then I take each frame of that video and crop the image to a vertical line of pixels right in the middle of the frame. Then with the concat method, I put one image after another until I have put all the cropped frames together.
Here is the video I used to create the image on the top, notice that it needs to be as centered as possible and the head-spinning around the middle axis of the screen.
I can't imagine why would someone want to use my code but just in case you want to do it the steps you need to follow:
- Install python (https://www.python.org/downloads/) and OpenCV (https://opencv.org/)
- Change the folder paths to the place where your folders are located.
- Put your video in the folder video and called it video, or it won't work.
- Run it clicking in the main.py file
I will try to make a unwrap.exe so it is easier for everyone to use this stupid program without haveng to go through the hassle of installing shit, if I manage to do I will edit this entry or just create another one.
Btw, this was just an excuse to learn to use OpenCV and I think that goal was fulfilled, but now I am going to try to find a shop that prints this image as wrapping paper to make gifts (if I finally do it I will keep it updated)
Before you go, a zip file with the code and the folders already created, the folder paths you need to use to paste into the code are these ones.
https://drive.google.com/file/d/1RemD2ECaI5P_vU-o-z_lDRSNa8HVr9b9/view?usp=sharing
It is my drive don't do weird things there please
Hope you liked my first entry,
Pablo
No, I don't like it
ReplyDelete