0

I get this error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor, file /opt/conda/conda-bld/opencv_1491943704081/work/opencv-3.1.0/modules/imgproc/src/color.cpp, line 7456
Traceback (most recent call last):
    old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
cv2.error: /opt/conda/conda-bld/opencv_1491943704081/work/opencv-3.1.0/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor


Process finished with exit code 1

for this code

import numpy as np
import cv2

#cap = cv2.VideoCapture('slow.flv')
cap = cv2.VideoCapture('slow_traffic_small.mp4')


# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 100,
                       qualityLevel = 0.3,
                       minDistance = 7,
                       blockSize = 7 )

# Parameters for lucas kanade optical flow
lk_params = dict( winSize  = (15,15),
                  maxLevel = 2,
                  criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

# Create some random colors
color = np.random.randint(0,255,(100,3))

# Take first frame and find corners in it
ret, old_frame = cap.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)

# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)

while(1):
    ret,frame = cap.read()
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # calculate optical flow
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)

    # Select good points
    good_new = p1[st==1]
    good_old = p0[st==1]

    # draw the tracks
    for i,(new,old) in enumerate(zip(good_new,good_old)):
        a,b = new.ravel()
        c,d = old.ravel()
        mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)
        frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1)
    img = cv2.add(frame,mask)

    cv2.imshow('frame',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

    # Now update the previous frame and previous points
    old_gray = frame_gray.copy()
    p0 = good_new.reshape(-1,1,2)

cv2.destroyAllWindows()
cap.release()

How should I fix it?

Here is the link to video: http://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4 Here's the link to original code: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_video/py_lucas_kanade/py_lucas_kanade.html#lucas-kanade

Added the item mentioned:

while(1):
    ret,frame = cap.read()
    if not ret: continue

And still getting error:

cv2.error: /opt/conda/conda-bld/opencv_1491943704081/work/opencv-3.1.0/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor

UPDATE: I tried this code which is just a video capture:

import cv2
import numpy as np
cap = cv2.VideoCapture(0)

ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255

while(1):
   ret, frame2 = cap.read()
   next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)

   flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

   mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
   hsv[...,0] = ang*180/np.pi/2
   hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
   bgr = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)

   cv2.imshow('frame2',bgr)
   k = cv2.waitKey(30) & 0xff
   if k == 27:
       break
   elif k == ord('s'):
       cv2.imwrite('opticalfb.png',frame2)
       cv2.imwrite('opticalhsv.png',bgr)
   prvs = next

cap.release()
cv2.destroyAllWindows()

And I am getting this error:

/usr/local/anaconda3/bin/python /home/grad3/jalal/PycharmProjects/hw4_cs585/video_capture.py
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor, file /opt/conda/conda-bld/opencv_1491943704081/work/opencv-3.1.0/modules/imgproc/src/color.cpp, line 7456
Traceback (most recent call last):
  File "/home/grad3/jalal/PycharmProjects/hw4_cs585/video_capture.py", line 6, in <module>
    prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
cv2.error: /opt/conda/conda-bld/opencv_1491943704081/work/opencv-3.1.0/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor


Process finished with exit code 1

Also I have:

[jalal@goku hw4_cs585]$ ffmpeg -codecs >> codecs.txt
ffmpeg version 2.6.8 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-4)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfaac --enable-nonfree --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100

https://pastebin.com/HSyHSsEZ

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • @miki: please check the update! the provided solution, didn't help me! – Mona Jalal Oct 29 '17 at 18:25
  • What's the shape of your `frame`? – Miki Oct 29 '17 at 18:28
  • 1
    The linked solution is in the case of webcams, you're reading from a file. Always check if the `VideoCapture` is opened or not. `if not cap.isOpened(): print('not opened')`. I think the problem is with your ffmpeg not being able to decode the file. – zindarod Oct 29 '17 at 18:31
  • ok cap is not opened what should I do? I just literally want to simply follow an opencv tutorial – Mona Jalal Oct 29 '17 at 18:34
  • @Mona I don't have any link to that video – Miki Oct 29 '17 at 18:34
  • shouldn't it regardless work for other videos in opencv website? – Mona Jalal Oct 29 '17 at 18:34
  • I can't find a link to slow.flv as told in the original tutorial. The only difference between the code here and there is not using slow.flv – Mona Jalal Oct 29 '17 at 18:35
  • 1
    You are missing some codecs, or, more likely, using the wrong path to the video file – Miki Oct 29 '17 at 18:35
  • so I gave the entire path `cap = cv2.VideoCapture('/home/grad3/jalal/PycharmProjects/hw4_cs585/slow_traffic_small.mp4')` and still not getting it work. This is a new CentOS machine and I am not admin. Which codecs should be installed? How do I know that? – Mona Jalal Oct 29 '17 at 18:37
  • the new duplicate target should help – Miki Oct 29 '17 at 18:40
  • Do you even have ffmpeg installed? I don't think there's any official ffmpeg repo for CentOS. What is the output of `ffmpeg -codecs >> codecs.txt`? – zindarod Oct 29 '17 at 18:45
  • @Zindarod https://pastebin.com/YGk2DDCi – Mona Jalal Oct 29 '17 at 18:46
  • You've to upload the file `codecs.txt`, not what's on the console. :D – zindarod Oct 29 '17 at 18:48
  • please check the updates! – Mona Jalal Oct 29 '17 at 18:51
  • @Zindarod https://pastebin.com/HSyHSsEZ – Mona Jalal Oct 29 '17 at 18:54
  • Your ffmpeg has the capability to decode the file, maybe the file is corrupt. Check your code with this file: http://samples.mplayerhq.hu/MPEG-4/Concession_LAN_800k.mp4 – zindarod Oct 29 '17 at 19:03
  • @Zindarod well, no luck with that too! so frustrating cap = cv2.VideoCapture('/home/grad3/jalal/PycharmProjects/hw4_cs585/Concession_LAN_800k.mp4') :( – Mona Jalal Oct 29 '17 at 19:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157760/discussion-between-zindarod-and-mona-jalal). – zindarod Oct 29 '17 at 19:07

0 Answers0