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()
Comments
Please log in or sign up to comment.