Last active
March 28, 2019 18:09
-
-
Save bart02/b98076a59c8066184a1eb24d8533ec2c 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 as cv | |
| import numpy as np | |
| def check(frame, mask): | |
| contours = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE) | |
| contours = contours[1] | |
| if contours: | |
| contours = sorted(contours, key=cv.contourArea, reverse=True) | |
| # cv.drawContours(frame, contours, 0, (255, 255, 0), 3) | |
| contour = contours[0] | |
| # x, y, w, h = cv.boundingRect(contour) | |
| eps = 0.01 * cv.arcLength(contour, True) | |
| approx = cv.approxPolyDP(contour, eps, True) | |
| approx = np.squeeze(approx, axis=1) | |
| points_y = [] | |
| for point in approx: | |
| x, y = point | |
| points_y.append(y) | |
| points_y.sort() | |
| gear_h = points_y[-1] - points_y[0] | |
| last = -1 | |
| for tek_y in points_y[::-1]: | |
| if last > -1: | |
| if last - tek_y > 20: | |
| last = tek_y | |
| break | |
| last = tek_y | |
| up_h = last - points_y[0] | |
| # print(up_h / gear_h) | |
| if up_h / gear_h > 0.555: | |
| geartype = 1 | |
| else: | |
| geartype = 2 | |
| cv.line(frame, (0, points_y[0]), (800, points_y[0]), (255, 0, 0), 3) | |
| cv.line(frame, (0, last), (800, last), (255, 255, 0), 3) | |
| cv.line(frame, (0, points_y[-1]), (800, points_y[-1]), (0, 255, 0), 3) | |
| else: | |
| geartype = 0 | |
| return frame, geartype | |
| def classificate(filename): # only for telegram | |
| img = cv.imread(filename) | |
| resized = cv.resize(img, (800, 600), interpolation=cv.INTER_AREA) | |
| frame = resized.copy() | |
| hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) | |
| mask = cv.inRange(hsv, tuple(arrdots[0]), tuple(arrdots[1])) | |
| frame, t = check(frame, mask) | |
| return t | |
| def click_and_crop(event, x, y, flags, param): | |
| global arrdots, first, hsv | |
| if event == cv.EVENT_LBUTTONDOWN: | |
| if first: | |
| arrdots = [[255, 255, 255], [0, 0, 0]] | |
| first = False | |
| color = hsv[y, x] | |
| for i in range(3): | |
| old = arrdots[0][i] | |
| arrdots[0][i] = int(min(arrdots[0][i], color[i])) | |
| if arrdots[0][i] != old: | |
| arrdots[0][i] -= 15 | |
| old = arrdots[1][i] | |
| arrdots[1][i] = int(max(arrdots[1][i], color[i])) | |
| if arrdots[1][i] != old: | |
| arrdots[1][i] += 15 | |
| arrdots = [[0, 30, 0], [255, 255, 85]] | |
| first = True | |
| if __name__ == '__main__': | |
| img = cv.imread('3.jpg') | |
| resized = cv.resize(img, (800, 600), interpolation=cv.INTER_AREA) | |
| frame = resized.copy() | |
| cv.imshow("Image", frame) | |
| cv.setMouseCallback('Image', click_and_crop) | |
| hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) | |
| while True: | |
| frame = resized.copy() | |
| mask = cv.inRange(hsv, tuple(arrdots[0]), tuple(arrdots[1])) | |
| frame, t = check(frame, mask) | |
| print(t) | |
| cv.imshow("Image", frame) | |
| if cv.waitKey(1) & 0xFF == ord('q'): | |
| break | |
| cv.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment