def houghlines(im,h):
#im = cv2.imread('2.jpg')
#ret,gray = cv2.threshold(im,40,255,cv2.THRESH_TOZERO_INV)
#gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
#edges = cv2.Canny(gray,10,200)
def getKey(item):
return abs(item[1]-item[3])
edges = r(im)
lines = cv2.HoughLines(edges,20,np.pi/190,100)
horizontal = []
for line in lines:
for rho,theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b)) # Here i have used int() instead of rounding the decimal value, so 3.8 --> 3
y1 = int(y0 + 1000*(a)) # But if you want to round the number, then use np.around() function, then 3.8 --> 4.0
x2 = int(x0 - 1000*(-b)) # But we need integers, so use int() function after that, ie int(np.around(x))
y2 = int(y0 - 1000*(a))
#cv2.line(im,(x1,y1),(x2,y2),(0,255,0),2)
#print(str(x1) + " " + str(y1) + " " + str(x2) + " " + str(y2))
horizontal.append((x1,y1,x2,y2))
#cv2.imshow('houghlines',im)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
horizontal = sorted(horizontal,key=getKey)
i = 0
while True:
cv2.line(im,(horizontal[i][0],horizontal[i][1]),(horizontal[i][2],horizontal[i][3]),(200,0,0),2)
cv2.imshow('houghlines',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("line.jpg",im)
average = (horizontal[i][1]+horizontal[i][3])/2.0
percent = average/h
actual = 100-(percent*100)
if actual > 80 or actual < 20:
i += 1
print(actual)
elif actual <30:
print("the coffee pot is getting low " + str(actual) + "% full!")
else:
print("the coffee pot is " + str(actual) + "% full!")
onlineCoffee.updateCoffeeSite("The coffee pot is " + str(int(actual)) + "% full!")
break
评论列表
文章目录