Commit 52902972 authored by P.M.P.C Bandara's avatar P.M.P.C Bandara

Number of plants added

parent 80e70fec
import cv2
from object_detector import *
import numpy as np
# Load Aruco detector
parameters = cv2.aruco.DetectorParameters_create()
aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_5X5_50)
# Load Object Detector
detector = HomogeneousBgDetector()
# Load Cap
cap = cv2.VideoCapture('http://192.168.8.126:8080/video') #'http://192.168.8.126:8080/video'
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
i=1
print("Type C for Chille, G for Gotukola,T for Tomato and L for Ladies fingers")
name = input("Enter a plant variant : ")
print(name)
while i==1:
_, img = cap.read()
# Get Aruco marker
corners, _, _ = cv2.aruco.detectMarkers(img, aruco_dict, parameters=parameters)
if corners:
# Draw polygon around the marker
int_corners = np.int0(corners)
cv2.polylines(img, int_corners, True, (0, 255, 0), 5)
# Aruco Perimeter
aruco_perimeter = cv2.arcLength(corners[0], True)
# Pixel to cm ratio
pixel_cm_ratio = aruco_perimeter / 20
contours = detector.detect_objects(img)
# Draw objects boundaries
for cnt in contours:
# Get rect
rect = cv2.minAreaRect(cnt)
(x, y), (w, h), angle = rect
# Get Width and Height of the Objects by applying the Ratio pixel to cm
object_width = w / pixel_cm_ratio
object_height = h / pixel_cm_ratio
# Display rectangle
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.circle(img, (int(x), int(y)), 5, (0, 0, 255), -1)
cv2.polylines(img, [box], True, (255, 0, 0), 2)
cv2.putText(img, "Width {} cm".format(round(object_width, 1)), (int(x - 100), int(y - 20)), cv2.FONT_HERSHEY_PLAIN, 2, (100, 200, 0), 2)
cv2.putText(img, "Length {} cm".format(round(object_height, 1)), (int(x - 100), int(y + 15)), cv2.FONT_HERSHEY_PLAIN, 2, (100, 200, 0), 2)
tot = object_width*object_height
if name == 'C':
numb = int(tot//(60*45))
print("Total number of Chili plants: "+str(numb))
elif name == 'G':
numb = int(tot//(22*30))
print("Total number of Gotukola plants: "+str(numb))
elif name == 'L':
numb = int(tot//(75*40))
print("Total number of Ladies finger plants: "+str(numb))
elif name == 'T':
numb = int(tot//(15*20))
print("Total number of Tomato plants: "+str(numb))
i=i+1
cv2.imshow("Image", img)
key = cv2.waitKey(1)
if key == 27:
break
#cap.release()
#cv2.destroyAllWindows()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment