Bob Lander
Bob Lander

Reputation: 3

OpenCv issues in Python Resutling 'int' object is not callable

Situation: I am trying to find the differences between frames of my video. I have 35000 such frames, and all of them have a name like FRAMENUMBER.jpg. They are in the directory but are unsorted. This is my code that I wrote to find the differences but I have several issues.

import os
import cv2
import pandas as pd 
frame = []
pic = []
directory = r'/home/kjo2/video'
for filename in os.listdir(directory):
    if filename.endswith(".jpg"):
        bob = os.path.join(filename)
        pic.append(bob)
    else:
        continue
pd.DataFrame(pic).to_csv("boo.csv")
for counter in range(1,35000):
    if counter == 35000:
        img1 = cv2.IMREAD_GRAYSCALE("%s",pic[(counter - 1)])
        img2 = cv2.IMREAD_GRAYSCALE("%s",pic[0])
        diff = cv2.absdiff(img1, img2)
        frame.append(diff)
    else:
        img1 = cv2.IMREAD_GRAYSCALE("%s",pic[(counter - 1)])
        img2 = cv2.IMREAD_GRAYSCALE("%s",pic[counter])
        diff = cv2.absdiff(img1, img2)
        frame.append(diff)
pd.DataFrame(frame).to_csv("foo.csv")

Errors:

  1. Everything upto oupto the second for loop works, but there is an issue with the pic array. Since the files are unsorted the array is also unsorted. What would be an easy way to sort these files in ascending order.
  2. When I try to read the image using opencv module, it always throws an error
File "frame.py", line 20, in <module>
    img1 = cv2.IMREAD_GRAYSCALE("%s",pic[(counter - 1)])
TypeError: 'int' object is not callable

I am trying to iterate over the files and compare the differences between them. What is wrong with my for loop or what is a better solution to this problem?

I am using Python 3.6.9 (default, Jul 17 2020, 12:50:27) [GCC 8.4.0] on linux.

Upvotes: 0

Views: 1473

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

That error is telling you that cv2.IMREAD_GRAYSCALE is a number. The correct way to use it is to do something like

img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)

or, in your case,

img1 = cv2.imread(pic[counter - 1], cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(pic[counter], cv2.IMREAD_GRAYSCALE)

Since you're comparing adjacent images, you can save half the reads (and simplify the code a bit) by reusing one of the images.

img1 = cv2.imread(pic[0], cv2.IMREAD_GRAYSCALE)
for i in range(1, len(pic)):
    img2 = cv2.imread(pic[i], cv2.IMREAD_GRAYSCALE)
    frame.append(cv2.absDiff(img1, img2))
    img1 = img2

    

Upvotes: 1

Related Questions