Skip to main content

Unwrapping someone's face to make wrapping paper

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











 

Comments

Post a Comment

Popular posts from this blog

A first approach to IoT, connecting my 3D printer to the internet

My first approach to the IoT, connecting my 3D printer to the internet IoT is one of those fancy words that people like to talk about in conferences and in TedTalks without (apparently) having too much idea of what it is all about. Set up to manage the 3D printer through the internet This one is going to be a short entry where I don't go through code or anything, just wanted to talk about a bit about how I connected my 3D printer to the internet.  I've been in the 3D printing thing for a while now, about a year and I haven't stopped printing ever since I bought my Ender 3. Fortunately enough, I live in a big house where my room/working place is on the fourth floor and my 3D printing is on the first one. You might be thinking as well: "OK Pablo but where do you want to bring us? Go to the point" Well, as you might have noticed there are two floors in betw...

Advent of Code, day 2

We made it to Day 2 of Advent of Code I have kept my promise for 2 days in a row. As weird as it seems for this day and age, I have kept my promise and today is the day 2 of the Advent of Code, this time with elves involved. There you go, the statement of today's first problem down below: And usual, my solution: import pandas as pd import re import numpy as np filepath = "/content/input 1.txt" with open (filepath, 'r' ) as file :     lines = [line.strip() for line in file .readlines()] lines_split = [] for line in lines:   token = re.findall(r '\w+|[^\s\w]' , line)   lines_split.append(token) blue_list = [] green_list = [] red_list = [] for line in lines_split:   blue,green,red = 0 , 0 , 0   for ix, word in enumerate (line):     if word == "blue" :       if blue < int (line[ix -1 ]):         blue = int (line[ix -1 ])     if word == "green" :       if green < int (...

How many paths the snake in the Snake game can take?

How many paths the snake in the Snake game can take? I was given this challenge last week and I thought I would share my way of solving it with you all Y'all know the famous game "Snake", so no need for an introduction now. The question is easy: given the position of the snake within a map of known size and the number of movements the snake can do, how many distinct paths can the snake follow without intersecting itself and without going out of the map's boundaries? Maybe once you go over it the question is not as easy as it sounds, so let's illustrate it with an example. Let's imagine we have a map that contains 4 rows and 3 columns, like the one below. And let's imagine that we have a snake whose body occupies the following cells: [(2,2),(3,2),(3,1),(3,0),(2,0),(1,0),(0,0)]. The figure on the left represents the empty map and the one on the right represents the one with the snake, where the numbers are the positions of each "piece" of the snake...