Created
July 24, 2023 04:18
-
-
Save cvramanan/4d3ba2e7eb2a0259d4276d143ad6cef9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import cv2 | |
| import numpy as np | |
| import time | |
| class Conedetection: | |
| def __init__(self): | |
| self.previousSum = 0 | |
| self.lineSensorWidth = 10 | |
| self.previousFrame = np.ones((720,1280,3),dtype = "uint8") | |
| self.coneThreshold = 300000 | |
| self.backSub = cv2.createBackgroundSubtractorKNN() | |
| self.framePassingTime = time.time() | |
| pass | |
| def detectCone(self,image): | |
| # cv2.imshow("uv image", image) | |
| img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
| img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
| display_image = img.copy() | |
| rectangle_coordinates = (img.shape[1]//2+100,0),(img.shape[1]//2+150,img.shape[0]//2) | |
| # img = img[:,600:] | |
| img = img[rectangle_coordinates[0][1]:rectangle_coordinates[1][1],rectangle_coordinates[0][0]:rectangle_coordinates[1][0]] | |
| # print(img.shape) | |
| (thresh, img_bin) = cv2.threshold(img, 1, 255,cv2.THRESH_BINARY| cv2.THRESH_OTSU) | |
| # (thresh, img_bin) = cv2.threshold(img, 1, 255,cv2.THRESH_BINARY) | |
| backSubOutput = self.backSub.apply(img_rgb, learningRate=-1) | |
| img_bin_analysis = img_bin.copy() | |
| img_bin_fullsum = img_bin.copy() | |
| display_image = cv2.rectangle(display_image,rectangle_coordinates[0],rectangle_coordinates[1],(0,255,0),3) | |
| cv2.imshow("thres", img_bin_fullsum) | |
| cv2.imshow("color image", display_image) | |
| y,x=img.shape | |
| img_bin_fullsum = cv2.flip(img_bin_fullsum, 0) | |
| # cv2.imshow("test uv full",img_bin_fullsum) | |
| fullSum = cv2.resize(img_bin_fullsum,(100,100)) | |
| # cv2.imshow("test uv",fullSum) | |
| x_offset = 0 | |
| fullSum = fullSum[20:-20,40+x_offset:-40+x_offset] | |
| # cv2.imshow("test uv",fullSum) | |
| cv2.waitKey(1) | |
| show_full = cv2.resize(fullSum,(500,500)) | |
| # cv2.imshow("test uv",show_full) | |
| fullSum = np.sum(fullSum)/255 | |
| # print(fullSum,fullSum.shape) | |
| fullThres = (100*100)*0.05 | |
| # if sumA >= self.coneThreshold/2 and sumB >= self.coneThreshold/2 and self.previousSum < self.coneThreshold: | |
| if fullSum > fullThres and self.previousSum < fullThres and (time.time()-self.framePassingTime) > 0.5: | |
| print("------------------------------------------------") | |
| print("Image taken",fullSum," ",self.previousSum," ",(time.time()-self.framePassingTime)) | |
| print("-------------------------------------------------") | |
| # print(self.coneThreshold) | |
| # print("The sum is ",sum) | |
| # cv2.imwrite("./uv_live/"+str(time.time())+"threshold"+str(sum)+".jpg",image) | |
| # print("Taking image") | |
| imb_bin_color = cv2.cvtColor(img_bin_analysis,cv2.COLOR_GRAY2BGR) | |
| # cv2.rectangle(imb_bin_color,(x11,y1-imgBinoff),(x12,y2-imgBinoff),(255,0,0),3) | |
| # cv2.rectangle(imb_bin_color,(x21,y1+imgBinoff),(x22,y2+imgBinoff),(0,255,0),3) | |
| # position_analysis_image = np.hstack([image,imb_bin_color,self.previousFrame]) | |
| # cv2.imwrite("./position/analysis/uv_"+str(time.time())+".jpg",position_analysis_image) | |
| self.framePassingTime = time.time() | |
| # self.previousSum = sumA+sumB | |
| return True , image | |
| # self.previousSum = sumA+sumB | |
| self.previousSum = fullSum | |
| self.previousFrame = image.copy() | |
| return False,np.ones((640,480,3),dtype = "uint8") | |
| if __name__ == "__main__": | |
| import os | |
| link = "tip-setup.avi" | |
| out_dir = "./conetip/" | |
| os.makedirs(out_dir, exist_ok=True) | |
| video = cv2.VideoCapture(link) | |
| conedetection = Conedetection() | |
| while True: | |
| ret,frame = video.read() | |
| if type(frame) != type([]): | |
| uvConePresence, uvConeImage = conedetection.detectCone(frame) | |
| if uvConePresence == True: | |
| cv2.imwrite(out_dir+str(time.time())+".jpg",uvConeImage) | |
| # cv2.imshow("Cone",uvConeImage) | |
| print("Cone captures") | |
| cv2.imshow("frame",frame) | |
| cv2.waitKey(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment