python, cv2.imshow(), raspberryPi and a black screen

Viewed 4394

Currently trying write code with a GUI which will allow for toggling on/off image processing. Ideally the code will allow for turning on/off window view, real time image processing (pretty basic), and controlling an external board.

The problem I'm having revolves around the cv2.imshow() function. A few months back I made a push to increase processing rates by switching from picamera to cv2 where I can perform more complex computations like background subtraction without having to call python all the time. using the bcm2835-v4l2 package, I was able to pull images directly from the picamera using cv2.

fast forward 6 months and while trying to update the code, I find that the function cv2.imshow() does not display correctly anymore. I thought it might be a problem with bcm2835-v4l2 but tests using matplotlib show that the connection is fine. it appears to have everything to do with cv2.imshow() or so I guess.

I am actually creating a separate thread using the threading module for image capture and I am wondering if this could be the culprit. I don't think so though as typing in the commands

import cv2
camera = cv2.VideoCapture(0)
grabbed,frame = camera.read()
cv2.imshow(frame)

produces the same black screen

Down below is my code I am using (on the RPi3) and some images show the error and what is expected.

as for reference here are the details about my system

Raspberry pi3
raspi stretch
python 3.5.1
opencv 3.4.1

Code

import cv2
from threading import Thread
import time
import numpy as np
from tkinter import Button, Label, mainloop, Tk, RIGHT

class GPIOControllersystem:
    def __init__(self,OutPinOne=22, OutPinTwo=27,Objsize=30,src=0):
        self.Objectsize = Objsize

        # Build GUI controller
        self.TK = Tk()                                                          # Place TK GUI class into self

        # Variables
        self.STSP = 0
        self.ShutdownVar = 0
        self.Abut = []
        self.Bbut = []
        self.Cbut = []
        self.Dbut = []

        # setup pi camera for aquisition
        self.resolution = (640,480)
        self.framerate = 60

        # Video capture parameters
        (w,h) = self.resolution
        self.bytesPerFrame = w * h

        self.Camera = cv2.VideoCapture(src)
        self.fgbg = cv2.createBackgroundSubtractorMOG2()

    def Testpins(self):
        while True:
            grabbed,frame = self.Camera.read()
            frame = self.fgbg.apply(frame)

            if self.ShutdownVar ==1:
                break
            if self.STSP == 1:
                pic1, pic2 = map(np.copy,(frame,frame))
                pic1[pic1 > 126] = 255
                pic2[pic2 <250] = 0
                frame = pic1
            elif self.STSP ==1:
                time.sleep(1)
            cv2.imshow("Window",frame)
        cv2.destroyAllWindows()

    def MProcessing(self):
        Thread(target=self.Testpins,args=()).start()
        return self

    def BuildGUI(self):
        self.Abut = Button(self.TK,text = "Start/Stop System",command = self.CallbackSTSP)
        self.Bbut = Button(self.TK,text = "Change Pump Speed",command = self.CallbackShutdown)
        self.Cbut = Button(self.TK,text = "Shutdown System",command = self.callbackPumpSpeed)
        self.Dbut = Button(self.TK,text = "Start System",command = self.MProcessing)

        self.Abut.pack(padx=5,pady=10,side=RIGHT)
        self.Bbut.pack(padx=5,pady=10,side=RIGHT)
        self.Cbut.pack(padx=5,pady=10,side=RIGHT)
        self.Dbut.pack(padx=5,pady=10,side=RIGHT)
        Label(self.TK, text="Controller").pack(padx=5, pady=10, side=RIGHT)
        mainloop()

    def CallbackSTSP(self):
        if self.STSP == 1:
            self.STSP = 0
            print("stop")
        elif self.STSP == 0:
            self.STSP = 1
            print("start")

    def CallbackShutdown(self):
        self.ShutdownVar = 1

    def callbackPumpSpeed(self):
        pass

if __name__ == "__main__":
    GPIOControllersystem().BuildGUI()

Using matplotlib.pyplot.imshow(), I can see that the connection between the raspberry pi camera and opencv is working through the bcm2835-v4l2 connection. Image of frame captured using system but plotting with matplotlib

However when using opencv.imshow() the window result in a blackbox, nothing is displayed. enter image description here

Update: so while testing I found out that when I perform the following task

import cv2
import matplotlib

camera = cv2.VideoCapture(0)
grab,frame = camera.read()
matplotlib.pyplot.imshow(frame)

grab,frame = camera.read()
matplotlib.pyplot.imshow(frame)

update was solved and not related to the main problem. This was a buffering issue. Appears to have no correlation to cv2.imshow()

1 Answers

on a raspberry you should work with from picamera import PiCamera checkout pyimagesearch for that

Related