Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. # this version 26/02/2020
  2. # dependencies: opencv, numpy, numba
  3. # Copyright 2018-2020 Franz KRUHM
  4. # fkruhm at gee mail dot com
  5. #
  6. # You MAY NOT make use of this code or derivates for commercial purposes without
  7. # permission from above copyright holder.
  8.  
  9. import cv2
  10. import numpy as np
  11. from numba import jit
  12. from multiprocessing import Process, Pool, active_children
  13.  
  14. max_threads = 24
  15.  
  16. @jit(nopython=True, fastmath=True)
  17. def phaseLines(img):
  18.     y = 0
  19.     height, width = img.shape
  20.     phaseStart = 0.25
  21.     density = 0.25
  22.     phase = 0
  23.     xStart = width - 1
  24.     xEnd = -1
  25.     xDir = -1
  26.     while y < height - 1:
  27.         y += 1
  28.         phase = 1 - phaseStart
  29.         x = xStart
  30.         while x != xEnd:
  31.             x += xDir
  32.             on = 0
  33.             phase += img[y, x]/255*density
  34.             if phase >= 1:
  35.                 on = 1
  36.                 phase -= 1
  37.                 img[y, x] = 255
  38.             else:
  39.                 img[y, x] = 0
  40.  
  41. def phaseCRT(input_image):
  42.     img = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
  43.     img = cv2.bitwise_not(img)
  44.     phaseLines(img)
  45.     img_blur = cv2.GaussianBlur(img, (11, 11), 0)
  46.     img = cv2.addWeighted(img, 1, img_blur, 3, 0)
  47.     img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
  48.     img = cv2.applyColorMap(img, cv2.COLORMAP_OCEAN)
  49.     return img
  50.  
  51. # fix filepath
  52. def thread_do(name, frame):
  53.     cv2.imwrite("d:\\ballet\\frm"+str(name).zfill(4)+".png", phaseCRT(frame))
  54.  
  55. def wait_threads(max):
  56.     while len(active_children()) >= max:
  57.         time.sleep(0.01)
  58.  
  59. if __name__ == '__main__':
  60.     invid = cv2.VideoCapture("d:\\ballet_white.mp4")
  61.     i = 0
  62.     threads = list()
  63.     while (True):
  64.         ret, frame = invid.read()
  65.         if ret == True:
  66.             wait_threads(max_threads)
  67.             threads.append(Process(target=thread_do, args=(i, frame,)))
  68.             threads[-1].start()
  69.             i += 1
  70.         else:
  71.             print("done.")
  72.             break
  73.     invid.release()
  74.     cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement