0

I have been working on opencv lately. Installed it on my Ubuntu 16.04. I think it has a few problems. Whenever I try to run the function

cv2.imshow('frame',frame)

it poses this error

OpenCV Error: Unspecified error (The function is not implemented. 
Rebuild the library with Windows, GTK+ 2.x or Carbon support. 
If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /tmp/build/80754af9/opencv_1512687413662/work/modules/highgui/src/window.cpp, line 611
Traceback (most recent call last): 
     File "hands.py", line 12, in <module>
     cv2.imshow('frame',frame)
     cv2.error: /tmp/build/80754af9/opencv_1512687413662/work/modules/highgui/src/window.
cpp:611: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. 
    If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage

The code I tried to run is

import numpy as numpy
import cv2
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0)
bg = cv2.bgsegm.createBackgroundSubtractorMOG()

while True:
    ret,frame = cap.read()
    vid = bg.apply(frame)

    cv2.imshow('frame',frame)
    cv2.imshow('vid',vid)
    key = cv2.waitKey(0) & 0xff
    if key == 27:
        break

cap.release()
cap.destroyAllWindows()

I tried to google every possible thing and did everything that's possible but still aren't able to solve the issue.

I also tried using

matplotlib.pyplot.imshow('frame',frame) 
matplotlib.pyplot.show()

instead of

cv2.imshow('frame',frame)

but this gives the error

TypeError: unhashable type: 'numpy.ndarray'

to show the video captured from my webcam. It either shows just a image and an error at cv2.waitkey() instead of the video or this error. Is there any method to solve this error? or to implement cv2 GUI features?

Darshan
  • 13
  • 5

1 Answers1

0

Seems like you have a problem with your cv2 installation. I cannot help with that, but I would recommend reinstalling and making sure the correct version for your system (matching OS, bit depth and python version) is used.

As a workaround you may indeed use matplotlib to show the image. A minimal example would be

import cv2
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0)

plt.ion()
fig, ax = plt.subplots()
ret,frame = cap.read()
im = ax.imshow(frame)

while True:
    ret,frame = cap.read()
    im.set_data(frame)
    plt.pause(0.5)

plt.ioff()
plt.show()

Also see update frame in matplotlib with live camera preview

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks for the answer but I am getting this error. `Image data cannot be converted to float` – Darshan Dec 20 '17 at 07:04
  • The last line is not enough, one needs to know the full error traceback. – ImportanceOfBeingErnest Dec 20 '17 at 09:22
  • `matplotlib/__init__.py in inner(ax, *args, **kwargs) 1715 warnings.warn(msg % (label_namer, func.__name__), 1716 RuntimeWarning, stacklevel=2) -> 1717 return func(ax, *args, **kwargs) matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs) 5125 resample=resample, **kwargs) -> 5127 im.set_data(X)` – Darshan Dec 20 '17 at 10:12
  • `matplotlib/image.py in set_data(self, A) 605 if (self._A.dtype != np.uint8 and 606 not np.can_cast(self._A.dtype, float, "same_kind")): --> 607 raise TypeError("Image data cannot be converted to float")` --The complete error in cuts. – Darshan Dec 20 '17 at 10:16
  • It seems that VideoCapture does not return a valid image. Are you sure that there is some video stream to capture at address 0? – ImportanceOfBeingErnest Dec 20 '17 at 10:23
  • cv2.VideoCapture(0) is supposed to open my webcam and output the video captured. – Darshan Dec 20 '17 at 11:33
  • Yes, it is supposed to do that. And if it successfully does it, `frame` should be an image, which can be shown via `imshow`. (This is also what happens when I run the code.) That is why my suspicion is that something goes wrong when opening the videostream, such that it does not return a valid image. – ImportanceOfBeingErnest Dec 20 '17 at 12:02
  • I have done so much fixing the bug and I think I should erase everything out of my ubuntu and re install from the beginning . Can you please suggest something for it? – Darshan Dec 25 '17 at 12:45