Home>

I'm self-taught about image processing

Split/binarize video → Search for photos with large black areas → Delete images before the found image → Renumber the largest image as 0

I want to do the process

.

Error message

I've managed to complete it, but the program is bad
Processing takes too long
I want to process roughly 5000 files, but it takes about 10 minutes

Applicable source code
import os
import cv2
import numpy as np
import glob
from PIL import Image
import os

file_list = glob.glob ('// image_dir // * jpg')
for file in file_list:
 os.remove (file)

#Video coma
movie_name = 's'
movie = '// directory // 1_.avi'
cap = cv2.VideoCapture (movie)
count = 0
while True:
 ret, frame = cap.read ()
 if ret == True:
  count + = 1
  cv2.imwrite ('// image_dir //' + movie_name + '_' + str ("{0: 05d}". format (count)) + '.jpg', frame)
 else:
  break
#Get the largest image
for j in range (1,5000):
 filename = '// image_dir // s_% 05d.jpg'% (j)
 im = np.array (Image.open (filename) .convert ('L'))
#Threshold
 im_ = (im>180) * 255
 c = 0
# Explore the largest image
 max1 = 0
 for i in range (2048):
  if im_ [1, i] == 0:
   c = c + 1
 if max1<c:
  max1 = c
  d = j
  c = 0
print (d)
#Delete unnecessary images
for l in range (1, d):
 os.remove ('// image_dir // s_% 05d.jpg'% (l))
a = 0
directory = '// image_dir //'
files = os.listdir (directory)
for file in files:
 path = os.path.join (directory, file) #path
 name = str ('% 06d')% (a) + '. jpg'
 target = os.path.join (directory, name)
 os.rename (path, target)
 a = a + 1

I understand that it takes a lot of time to cut frames, but I want to shorten it a little more.

Teach us.

Appendix

import os
import cv2
import numpy as np
import glob
from PIL import Image
import os
import sys
import re
file_list = glob.glob ('// image_dir // * jpg')
for file in file_list:
  os.remove (file)

#Video coma
movie_name = 's'
movie = '// 11.avi'
cap = cv2.VideoCapture (movie)
count = 0
max1 = 0
c = 0
while True:
  ret, frame = cap.read ()
  if ret == True:
   cv2.imwrite ('// image_dir //' + movie_name + '_' + str ("{0: 05d}". format (count)) + '.jpg', frame)
#Binarization
   filename = '// image_dir // s_% 05d.jpg'% (count)
   im = np.array (Image.open (filename) .convert ('L'))
   im_ = (im>180) * 255
# Explore the largest image
   for i in range (2048):
    if im_ [100, i] == 0:
     c = c + 1
   if max1<c:
    max1 = c
    c = 0
    pil_img = Image.fromarray (im)
    pil_img.save ('// image_dir // s_00000.jpg')
    count = 1
   else:
    count + = 1
  else:
   break
  • Answer # 1

    It's not an answer, but I can't write in a long sentence to add/modify requests.

    Principles for speed improvement

    Optimize code that meets the requirements.
    ret, frame = cap.read ()reads the frame of the video andcv2.imwrite, butcv2.imwriteDo n’t you?

    Write test code
    Speeding up involves code changes, so write simple test code to behave the same as existing behavior.
    This prevents code destruction due to code changes.

    Determine the target time after optimization.
    Currently 5000 files are 10 minutes.
    If the final target time is not determined unless it is better to improve even 1 second, it is easy to get lost.
    * Due to the limitations of PC hardware, it is impossible in principle to exceed this speed.

    Get the profiler first without guessing
    Take the CPU profiler and memory profiler and check the bottleneck point.
    Since it is difficult to work with code if it is not functionalized, it is recommended that it be functionalized for each process.

    The pathlib module is useful for IO operations.

  • Answer # 2

    Once loaded
    "Split and binarize video"
    Do it,
    "Photos with large black areas"
    If you overwrite each time the is updated, you don't have to erase anything you don't need after you export all the images.

  • Answer # 3

    I haven't considered it in much detail, but come up with something.

    I use a lot of file reading/writing, but it is probably the biggest bottleneck. Is it possible to place it in memory even partially? After that, hide it with asyncio etc. (It is difficult)

    The search code is questionable. Is this processing if the number of 0s in the first row of the image array is large?max1 = 0is inside the loop.

    It has nothing to do with the main subject, but the indentation for each space is a little ...