0

I'm a beginner on Python and want to ask whether can I draw images of different function onto one video? Below is my practice code.

import numpy as np
import cv2
from multiprocessing import Process


cap = cv2.VideoCapture('C:/Users/littl/Desktop/Presentation/Crop_DownResolution.mp4')

def line_drawing():
while cap.isOpened():
    ret, img = cap.read()
    if ret is True:
        cv2.line  (img,(50,180),(380,180),(0,255,0),5)
        cv2.imshow('img',img)
        k = cv2.waitKey(1) & 0xff
        if k == 27:
            break
    else:
        break

    cap.release()
    cv2.destroyAllWindows()

def rectangle_drawing():
while cap.isOpened():
    ret, img = cap.read()
    if ret is True:
        cv2.rectangle(img,(180,0),(380,128),(0,255,0),3)
        cv2.imshow('img',img)
        k = cv2.waitKey(1) & 0xff
        if k == 27:
            break
    else:
        break

    cap.release()
    cv2.destroyAllWindows()

if __name__=='__main__':
    p1 = Process(target = rectangle_drawing)
    p1.start()
    p2 = Process(target = line_drawing)
    p2.start()

When I run the code, it gives me 2 tabs running the same video, one with the line drawn, the other with the rectangle drawn. How to I make both the rectangle and line to be on the video and have the functions separated instead of putting both in the same function?

1 Answers1

2

I won't be able to give you an answer with Python code but...

What you have is two different threads, both capturing data from the video feed independently, and drawing the elements on separate pieces of data.

What you need to do is Have one process that is just in charge of data capture from your video feed which then provides that data for the other two threads. You would likely need to look into Mutexes so that the two threads don't clash with each other.

Resources

There are quite a few questions on SO and the internet that will help you achieve this:

GPPK
  • 6,546
  • 4
  • 32
  • 57