def fire_detection(self, verbose=True):
"""
Detect the smoke and return the fire coordinate in the image
Parameters:
verbose (bool): whether to print status messages or not
Returns:
coord_fire (list):x,y image coordinates of the fire
"""
img = self.get_image_from_camera()
# Segment the image by color in HSV color space
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
# Range of the smoke
smoke_lower = np.array([0, 0, 168])
smoke_upper = np.array([172, 111, 255])
mask_fire = cv2.inRange(hsv, smoke_lower, smoke_upper)
fire_ratio = np.round(
(cv2.countNonZero(mask_fire))/(img.size/3)*100, 2)
if fire_ratio > 0.15: # Higher the fire ratio, higher the number of fire in the image
# Detect the contours on the binary image using cv2.CHAIN_APPROX_NONE
contours, _ = cv2.findContours(
image=mask_fire, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
# Approximate contours to polygons + get circles
contours_poly = [None]*len(contours)
centers = [None]*len(contours)
radius = [None]*len(contours)
radius_max = 0
for i, c in enumerate(contours):
contours_poly[i] = cv2.approxPolyDP(c, 3, True)
centers[i], radius[i] = cv2.minEnclosingCircle(
contours_poly[i])
# We keep only the biggest circle and > 3
if radius[i] > 3 and radius[i] > radius_max:
coord_fire = centers[i]
radius_max = radius[i]
if verbose:
print(
"fire detected, coordinates {}".format(centers[i]))
if verbose: # Draw polygonal contour + circles and save the image
drawing = img.copy()
for i in range(len(contours)):
color = (random.randint(0, 256), random.randint(
0, 256), random.randint(0, 256))
cv2.drawContours(drawing, contours_poly, i, color)
cv2.circle(drawing, (int(centers[i][0]), int(
centers[i][1])), int(radius[i]), color, 2)
cv2.imwrite("fire_detection.jpg", drawing)
return coord_fire
“keeps crashing” you say.
do you have any details?
what have you tried, to debug this, to get information about the crash?