Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mvi-sp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aneta Texler
mvi-sp
Commits
78098171
Commit
78098171
authored
6 years ago
by
Aneta
Browse files
Options
Downloads
Patches
Plain Diff
new main
parent
e6d7e696
Branches
part2
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
colorization/main.py
+87
-0
87 additions, 0 deletions
colorization/main.py
with
87 additions
and
0 deletions
colorization/main.py
0 → 100644
+
87
−
0
View file @
78098171
import
colorize
import
os
import
re
import
moviepy.editor
as
mp
import
cv2
def
subsample
(
input_file_path
):
clip
=
mp
.
VideoFileClip
(
input_file_path
)
clip_resized
=
clip
.
resize
(
height
=
360
)
clip_resized
.
write_videofile
(
os
.
path
.
join
(
'
DATA/videosDS/
'
,
input_file_path
.
replace
(
'
DATA/videos/
'
,
''
)))
def
video_to_frames
(
video_path
):
# create a folder to store extracted images
folder_name
=
'
DATA/frames/
'
+
video_path
.
replace
(
'
DATA/videosDS/
'
,
''
).
replace
(
'
.mp4
'
,
'
/
'
)
if
not
os
.
path
.
exists
(
folder_name
):
os
.
makedirs
(
folder_name
)
# extract frames
video_cap
=
cv2
.
VideoCapture
(
video_path
)
fps
=
video_cap
.
get
(
cv2
.
CAP_PROP_FPS
)
success
,
image
=
video_cap
.
read
()
count
=
0
while
True
:
success
,
image
=
video_cap
.
read
()
if
not
success
:
break
cv2
.
imwrite
(
os
.
path
.
join
(
folder_name
,
"
{:d}.jpg
"
.
format
(
count
)),
image
)
# save frame as JPEG file
count
+=
1
print
(
"
{} frames are extracted in {}.
"
.
format
(
count
,
folder_name
))
return
fps
def
numerical_sort
(
value
):
# sort frames from dir
numbers
=
re
.
compile
(
r
'
(\d+)
'
)
parts
=
numbers
.
split
(
value
)
parts
[
1
::
2
]
=
map
(
int
,
parts
[
1
::
2
])
return
parts
def
colorize_frames
(
greyscale_frames_folder
):
colored_frames_folder
=
'
DATA/colored_frames/
'
+
greyscale_frames_folder
.
replace
(
'
DATA/frames/
'
,
''
)
if
not
os
.
path
.
exists
(
colored_frames_folder
):
os
.
makedirs
(
colored_frames_folder
)
input_images
=
[
img
for
img
in
os
.
listdir
(
greyscale_frames_folder
)
if
img
.
endswith
(
"
.jpg
"
)]
input_images
.
sort
(
key
=
numerical_sort
)
count
=
0
for
input_image
in
input_images
:
colorize
.
main
(
os
.
path
.
join
(
greyscale_frames_folder
,
input_image
),
os
.
path
.
join
(
colored_frames_folder
,
"
{:d}.jpg
"
.
format
(
count
)))
count
+=
1
def
frames_to_video
(
frames_path
,
fps
):
video_folder
=
'
DATA/colored_videos/
'
+
frames_path
.
replace
(
'
DATA/colored_frames/
'
,
''
)
+
'
.avi
'
images
=
[
img
for
img
in
os
.
listdir
(
frames_path
)
if
img
.
endswith
(
"
.jpg
"
)]
images
.
sort
(
key
=
numerical_sort
)
frame
=
cv2
.
imread
(
os
.
path
.
join
(
frames_path
,
images
[
0
]))
height
,
width
,
layers
=
frame
.
shape
fourcc
=
cv2
.
VideoWriter_fourcc
(
*
'
mp4v
'
)
# codec
video
=
cv2
.
VideoWriter
(
video_folder
,
fourcc
,
fps
,
(
width
,
height
))
for
image
in
images
:
video
.
write
(
cv2
.
imread
(
os
.
path
.
join
(
frames_path
,
image
)))
cv2
.
destroyAllWindows
()
video
.
release
()
def
main
():
input_file_path
=
"
DATA/videos/chaplin2.mp4
"
# preserve the path, change only the file name
subsample
(
input_file_path
)
fps
=
video_to_frames
(
input_file_path
.
replace
(
'
videos
'
,
'
videosDS
'
))
colorize_frames
(
'
DATA/frames/
'
+
input_file_path
.
replace
(
'
DATA/videos/
'
,
''
).
replace
(
'
.mp4
'
,
''
))
frames_to_video
(
'
DATA/colored_frames/
'
+
input_file_path
.
replace
(
'
DATA/videos/
'
,
''
).
replace
(
'
.mp4
'
,
''
),
fps
)
if
__name__
==
'
__main__
'
:
main
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment