Skip to content

Instantly share code, notes, and snippets.

@serinuntius
Created June 21, 2016 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serinuntius/a706dca728975c14f7ce9afe6e07a66b to your computer and use it in GitHub Desktop.
Save serinuntius/a706dca728975c14f7ce9afe6e07a66b to your computer and use it in GitHub Desktop.
ごまちゃんさん専用マルチプロセッシングサンプル
# coding: UTF-8
import numpy as np
import cv2
import multiprocessing as mp
from random import randint
def rect(f, num, features_point):
cv2.rectangle(f, (10, 10), (50, 50), (randint(0, 255), randint(0, 255), randint(0, 255)), 3)
cv2.imwrite(str(num) + '.jpg', f)
features_point[num] = randint(0,100)
if __name__ == "__main__":
multi = True
cap = cv2.VideoCapture("test.mp4")
width = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
boxes = [[0, 0, width / 2, height / 2],
[width / 2, 0, width, height / 2],
[0, height / 2, width / 2, height],
[width / 2, height / 2, width, height]
]
while cap.isOpened():
ret, frame = cap.read()
if ret:
#cv2.imshow("test", frame)
if multi:
# 共有の変数
manager = mp.Manager()
features_point = manager.dict()
process = [mp.Process(target=rect, args=(frame[box[1]:box[3], box[0]:box[2]], i, features_point)) for
i, box in
enumerate(boxes)]
for p in process:
p.start()
for p in process:
p.join()
print(features_point)
else:
for i, box in enumerate(boxes):
cv2.rectangle(frame[box[1]:box[3], box[0]:box[2]], (10, 10), (50, 50),
(randint(0, 255), randint(0, 255), randint(0, 255)), 3)
cv2.imwrite(str(i) + '.jpg', frame[box[1]:box[3], box[0]:box[2]])
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment