0

I'm new to python 3.10. I have the below python code outputting my webcam screen using python. How would I duplicate the webcam output within the same window. One screen on the left of the window and the second screen on the right hand side of the window? Something simple with no buttons. Or so I thought ;) proving more difficult than anticipated!

and no, "Integrate OpenCV webcam into a Kivy user interface" does not answer the question. I dont want to add button using kivy, i want to duplicate the webcam in the same screen window instance. i tried using kivy with a box layout in 'horizontal' orientation, but cannot figure bring my stream into the 2 box horizontal box layout. (adding kivy code below)

import cv2
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout

cap=cv2.VideoCapture(0)
cap.release()
cv2.destroyAllWindows()
cap=cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame=cap.read()
    cv2.imshow('VR',frame)
    cv2.imshow('VR', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

class VRWindow(Screen):
    def cam_connect(self):
        cap=cv2.VideoCapture(0)
        while cap.isOpened():
            ret, frame=cap.read()
            cv2.imshow('VR',frame)
            # if cv2.waitKey(1) & 0xFF == ord('q'):
            #     break
    pass

class Window(Screen):
    pass

class ScreenManger(ScreenManager):
    pass

kv = Builder.load_file('vrgui.kv')

class VRApp(App):
     def build(self):
        layout = boxLayout()
        layout.orientation = 'horizontal'

        return kv

if __name__ == '__main__':
    VRApp().run()

kv file

ScreenManager:
    VRWindow:
    Window:

<VRWindow>:
    name: "first"


    FloatLayout:
        #orientation:'horizontal'
            Label:
            text: 'VRLeft'
            size: root.width, root.height
            pos_hint: {'x': 0, 'y': .0}
            size_hint: .5, 1.0
            root.cam_connect()
        Label:
            text: 'VRRight'
            size: root.width, root.height
            pos_hint: {'x': .5, 'y': .0}
            size_hint: .5, 1.0
            #root.cam_connect()
<Window>:
    name: "second"

    FloatLayout:
        #orientation:'horizontal'
        size: root.width, root.height
        pos_hint: {'x': 0, 'y': .0}
        size_hint: 1.0, 1.0
        #root.cam_connect()

Thank you for your time.

Nate
  • 1
  • 1
  • welcome. [tour], [ask], [mre]. -- the code you present shows no attempt to use kivy. – Christoph Rackwitz Dec 14 '22 at 11:21
  • here is my code using kivy. I think kivy is only used to create a user interface and arrange layouts. i don't think it is meant to import usb webcam screens. but i'm new and could be wrong. – Nate Dec 14 '22 at 12:11
  • `cv2.imshow` **can't be mixed with** kivy, or any other GUI library. GUI libraries **in general can't be mixed**. OpenCV is not a GUI library but it can use GUI, so its GUI APIs fall under that condition. you can use it with whatever you like, as long as you understand how the other GUI library works and how to get data into that GUI library. you will likely require threading. yes, that is possible in python. no, it's not "slow". – Christoph Rackwitz Dec 14 '22 at 13:23
  • Really, i need to research threading. Do you know any pthon examples online I can refer to see how this works or see it in action? – Nate Dec 14 '22 at 16:31
  • your question was flagged as a duplicate of another question. please check it out: https://stackoverflow.com/questions/37749378/integrate-opencv-webcam-into-a-kivy-user-interface if that leaves questions, please describe your questions relative to such examples. – Christoph Rackwitz Dec 14 '22 at 21:16
  • if you want to involve a thread: the thread only reads from the video device, then "sends" that new data to the GUI in whatever ways kivy requires it (I'm not familiar with kivy, see other question for example). when the thread needs to stop/end, you just set a flag to false, which the thread constantly checks and then ends its infinite loop and thus the entire thread. – Christoph Rackwitz Dec 14 '22 at 21:19
  • Christoph Rackwitz I know it was flagged as duplicate. Which is wrong, this is totally not a duplicate. I am trying replicate my usb cam output into 1 screen in a side by side layout. "https://stackoverflow.com/questions/37749378/integrate-opencv-webcam-into-a-kivy-user-interface" is not trying to do the same. We just happen to be using the similar cv2 code to interface to our usb cam. – Nate Dec 15 '22 at 02:00
  • i found this thread "https://stackoverflow.com/questions/60170110/use-python-and-opencv-to-play-two-camera-streams-picture-in-picture-style" where someone is trying to do a dual video feed. He found an example hat was provided, but I cannot find the example. Th examples mentioned in this thread are overlaying pics, not video feeds. Is anyone else able to see this example he is referring to? This should help do what i am trying to do – Nate Dec 15 '22 at 02:26

0 Answers0