NARENDRANRavivarma SivathasanSarim Ahmed KhalilYee Han XiangXuan
Published

Fruit Bunch Ripeness Detection Using Colour Detection Method

The hovergames project is programmed to fly around the palm plantation and detect ripeness of fruit on the drone using colour detection.

IntermediateShowcase (no instructions)50 days215
Fruit Bunch Ripeness Detection Using Colour Detection Method

Things used in this project

Hardware components

KIT-HGDRONEK66
NXP KIT-HGDRONEK66
×1
Holybro ik-telemetry-radio-v3
×1

Software apps and online services

QGroundControl
PX4 QGroundControl

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver

Story

Read more

Schematics

JTAG and Reset, Boot Modes and SD Card

NAVQ-Plus Board Block Diagram

SOM Connectors

USB 3.0/2.0 , Type C, Dual Role

Main PWR

2.4/5 GHz Wifi Module

Code

Fruit Bunch Ripeness Colour Detection

Python
This is a python code which uses OpenCV Library to use live camera feed and process it. Here it is used to isolate the colors using Hue, Saturation and value. Color isolation is used to detect ripe and unripe fruit bunch in this project.
import cv2
import numpy as np
import time

# Define the video capture device
cap = cv2.VideoCapture(0)

# Define the video codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

# Record videos in 15-minute intervals
while True:
    start_time = time.time()
    elapsed_time = 0
    while elapsed_time < 900:
        ret, frame = cap.read()
        ret, frame = cap.read()
    	key = cv2.waitKey(1) & 0xff
    	# if frame is read correctly ret is True
    	if not ret:
        	print("Can't receive frame (stream end?). Exiting ...")
        	break
    	if key == ord('p'):

        	while True:

            	key2 = cv2.waitKey(1) or 0xff

            	if key2 == ord('p'):
                	break

	# Color filter using HSV ranging code for ripe
    	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    	mask1 = cv2.inRange(hsv, (0, 170, 60), (22, 255, 255))
    	res = cv2.bitwise_and(frame, frame, mask=mask1)
    	cv2.imshow('result 1', res)
    	kernel = np.ones((5, 5), np.uint8)
    	closing = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, kernel)
    	erosion1 = cv2.erode(closing, kernel, iterations=2)
    	dilation = cv2.dilate(erosion1, kernel, iterations=4)
    	closing2 = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
    	closing4 = cv2.morphologyEx(closing2, cv2.MORPH_CLOSE, kernel)
    	dilation2 = cv2.dilate(closing4, kernel, iterations=6)
    	cv2.imshow('dilation 2', dilation2)
    	contours, hierarchy = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    	name = "ripe"

	#Color filter using HSV ranging code for Unripe
    	mask2 = cv2.inRange(hsv, (130, 30, 0), (170, 180, 255))
    	res2 = cv2.bitwise_and(frame, frame, mask=mask2)
    	cv2.imshow('result 2', res2)
    	closing1 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernel)
    	erosion2 = cv2.erode(closing1, kernel, iterations=2)
    	dilation1 = cv2.dilate(erosion2, kernel, iterations=4)
    	closing3 = cv2.morphologyEx(dilation1, cv2.MORPH_CLOSE, kernel)
    	closing5 = cv2.morphologyEx(closing3, cv2.MORPH_CLOSE, kernel)
    	closing7 = cv2.morphologyEx(closing5, cv2.MORPH_CLOSE, kernel)
    	dilation3 = cv2.dilate(closing7, kernel, iterations=6)
    	cv2.imshow('dilation 3', dilation3)
    	contour, hierarchy = cv2.findContours(dilation3, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
	#unripe annotation
    	name2 = "unripe"

	#Conour creation for ripe fruit bunch
    	for c in contours:
        	x, y, w, h = cv2.boundingRect(c)
        	if (cv2.contourArea(c)) > 10:
            	cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 5)
            	cv2.putText(frame, name, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

	#Contour creation for unripe fruit bunch
    	for c in contour:
        	x, y, w, h = cv2.boundingRect(c)
        	if (cv2.contourArea(c)) > 10:
            	cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 0, 200), 5)
            	cv2.putText(frame, name2, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (100, 0, 200), 2)
		if ret:
            	out.write(frame)
            	cv2.imshow('frame', frame)
        	if cv2.waitKey(1) == ord('q'):
            	break
        	elapsed_time = time.time() - start_time
    	out.release()
    	if cv2.waitKey(1) == ord('q'):
        	break
    	time.sleep(60)

# Release the capture and destroy all windows
cap.release()
cv2.destroyAllWindows()

Credits

NARENDRAN
20 projects • 22 followers
Contact
Ravivarma Sivathasan
0 projects • 1 follower
Contact
Sarim Ahmed Khalil
0 projects • 1 follower
Contact
Yee Han Xiang
0 projects • 0 followers
Started playing with quadcopter about 6 months
Contact
Xuan
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.