def find_palms3(uid,fieldid, templateName): import cv2 as cv import numpy as np import matplotlib.pyplot as plt from skimage import filters from overlappingArea import overlappingArea import os try: templates = [] allBoxes = [] toInit= 'rm -rf ' + uid +'/' + fieldid + '_highres_side_by_side.png' os.system(toInit) toInit = 'rm -rf ' + uid +'/' + fieldid + '_highres_num.png' os.system(toInit) for i in range(1,30): j = i tempName = 'opp' + str(j) + '.png' #print(tempName) templates.append(tempName) #templates.append('template.png') #templates = ['template.png', 'template2.png', 'template3.png', 'template4.png','template5.png','template6.png','template7.png'] totalTrees = 0 filename = uid + '/highres_' +str(fieldid)+'.png' # loading the image - let's take a look at it image = cv.imread(filename) #plt.imshow(image) img_rgb = image #hsv_image = cv.cvtColor(img_rgb, cv.COLOR_BGR2HSV) hsv_image = cv.applyColorMap(img_rgb, cv.COLORMAP_JET) hsv_image = cv.bitwise_not(hsv_image) img_gray = cv.cvtColor(hsv_image, cv.COLOR_BGR2GRAY) fileName = uid +'/' + fieldid + '_highres_gray.png' cv.imwrite(fileName,img_gray) for templateName in templates: #try: count = 0 templateName = 'OPP/' + templateName template = cv.imread(templateName) hsv_template = cv.applyColorMap(template, cv.COLORMAP_JET) hsv_template = cv.bitwise_not(hsv_template) template = cv.cvtColor(hsv_template, cv.COLOR_BGR2GRAY) w, h = template.shape[::-1] # All the 6 methods for comparison in a list res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED) #res = cv.matchTemplate(img_gray,template,cv.TM_CCORR_NORMED) #print('resss') #print(res) threshold = 0.2 fileName = uid +'/' + fieldid + '_highres_num.png' try: img_rgb = cv.imread(fileName) cv.imwrite(fileName,img_rgb) except: print('file not available') img_rgb = image loc = np.where( res >= threshold) for pt in zip(*loc[::-1]): l1 = [pt[0],pt[1]] r1 = [(pt[0]+w),(pt[1]+h)] isOverlap = False for singlePoint in allBoxes: l2 = singlePoint[0][0] r2 = singlePoint[0][1] isOverlap = overlappingArea(l1,r1,l2,r2) if isOverlap == True: break box_multiple = 0.05 if isOverlap == False: try: boxImg = img_rgb[pt[1]:pt[1]+round(box_multiple*w),pt[0]:pt[0]+round(box_multiple*h)] boxImg = cv.cvtColor(boxImg, cv.COLOR_BGR2HSV) boxImg = cv.cvtColor(boxImg, cv.COLOR_BGR2GRAY) rows,cols = boxImg.shape whitePixels = 0 boxSize = rows*cols for i in range(rows): for j in range(cols): pixelValue = boxImg[i,j] if pixelValue <=80: whitePixels = whitePixels+1 whiteProportion = whitePixels/boxSize #print(whiteProportion) if whiteProportion < 0.4: midX =round((0.5*(2*pt[0]+w))) midY =round((0.5*(2*pt[1]+h))) midPoint = (midX,midY) radius = 2 color = (0,0,255) thickness = -1 cv.circle(img_rgb,midPoint,radius,color,thickness) #cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2) count = count+1 newBox = [] newBox.append([l1,r1]) allBoxes.append(newBox) except: ar = 1 cv.imwrite(fileName,img_rgb) totalTrees = totalTrees + count print('FinalTrees:') print(totalTrees) HorizontalImgs = np.hstack([image,img_rgb]) fileName = uid +'/' + fieldid + '_highres_side_by_side.png' cv.imwrite(fileName,HorizontalImgs) except: totalTrees = 0 totalTrees = round(1.5*totalTrees,0) return totalTrees