Count fruit from tree image

Question:

Here I am writing python code to count fruit from tree image. For that I have installed

pip install opencv-python
pip install numpy
pip install opencv-contrib-python
I am using python 3.11.2
These three packages

import cv2
import numpy as np

image = cv2.imread('tree.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# look for fruit in image
fruits = cv2.findContours(gray, cv2.CV_RETR_LIST, cv2.CV_CHAIN_APPROX_SIMPLE)

count =0
for fruit in fruits:
    (x, y, w, h) = cv2.boundingRect(fruit)
    count += np.sum(gray[y: y + h, x:x + w])
print('--------------------{}'.format(count))

When I run code getting error:

python .count_fruit.py

Traceback (most recent call last):
  File "C:UsersHPOneDriveDesktopIshwar WorkMLcount_fruit.py", line 8, in <module>
    fruits = cv2.findContours(gray, cv2.CV_RETR_LIST, cv2.CV_CHAIN_APPROX_SIMPLE)       
                                    ^^^^^^^^^^^^^^^^
AttributeError: module 'cv2' has no attribute 'CV_RETR_LIST'

writing python code to count fruit from tree image. For that I have installed

pip install opencv-python
pip install numpy
pip install opencv-contrib-python
I am using python 3.11.2
These three packages

Asked By: Sneha Somwanshi

||

Answers:

Just change cv2.CV_RETR_LIST -> cv2.RETR_LIST and cv2.CV_CHAIN_APPROX_SIMPLE -> cv2.CHAIN_APPROX_SIMPLE

Answered By: Zero-nnkn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.