main.py 867 B

123456789101112131415161718192021222324252627
  1. import cv2
  2. from scipy.spatial import distance
  3. general_count = 0
  4. for i in range(1, 13):
  5. img = cv2.imread(f"./res/img ({i}).jpg")
  6. image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  7. _, thresh = cv2.threshold(image, 120, 255, 0)
  8. contours, _ = cv2.findContours(
  9. thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
  10. )
  11. p_count = 0
  12. for cnt in contours:
  13. x, y, w, h = cv2.boundingRect(cnt)
  14. points = cv2.boxPoints(cv2.minAreaRect(cnt))
  15. w_euclid = distance.euclidean(points[0], points[1])
  16. h_euclid = distance.euclidean(points[0], points[3])
  17. if (h_euclid > 3 * w_euclid and h_euclid > 900) or (
  18. w_euclid > 3 * h_euclid and w_euclid > 900
  19. ):
  20. p_count += 1
  21. general_count += p_count
  22. print(f"Img {i}, pencils count: {p_count}")
  23. print(f"Pencils total: {general_count}")