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

Advent of Code, day 1

 Day 1 of the Advent of Code A bit late for Christmas, isn't it? It's been a long time since I did my last post, some days of boredom have inspired me to get back at it. I am going to be trying to solve the last Christmas' Advent of Code. For those that you don't know the Advent of Code is an Advent calendar of small programming puzzles that can be solved the way you like, each day contains two puzzles in which the first is linked to the second. If you want to get more info about it, check out this link:  https://adventofcode.com/2023/about Without further ado, let's get at it, I'm going to copy down below the statement for the Day 1 Statement Input Basically we are given a long list of characters (the one shown in the picture) where each line contains numbers and letters, we first need to get just the numbers, then store somewhere else the first and last numbers in each row, and lastly sum all those values. A link to the input file:  https://adventofcode.com/20...

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 (...