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
Answers:
Just change cv2.CV_RETR_LIST
-> cv2.RETR_LIST
and cv2.CV_CHAIN_APPROX_SIMPLE
-> cv2.CHAIN_APPROX_SIMPLE
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
Just change cv2.CV_RETR_LIST
-> cv2.RETR_LIST
and cv2.CV_CHAIN_APPROX_SIMPLE
-> cv2.CHAIN_APPROX_SIMPLE