ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] ·s¤â°ÝÃD python3 & OpenCV 4.5.3 ª©¥» Ãö©ó°õ¦æµ{¦¡·|¥d¹yªº°ÝÃD

[µo°Ý] ·s¤â°ÝÃD python3 & OpenCV 4.5.3 ª©¥» Ãö©ó°õ¦æµ{¦¡·|¥d¹yªº°ÝÃD

³o¬O¤@­Ó¦b·Ó¤ù¤Wµeª½½u µM«á¶q´ú­È½uªø«×ªºµ{¦¡½m²ß
³o¬qµ{¦¡¥N½X¦³¤@­Ó°ÝÃD ¦b°õ¦æµ{¦¡ªº®É­Ô ·|¥d¹y
¦b·QÀ³¸Ó¬O °j°éªº°ÝÃD  ¨ç¦¡¤¤ ¦³¤@¬q :

        directory = 'C:/VSC_PY/GUI_AI/sand_train/text_dir/'  # ¥Ø¿ý¸ô®|
        total_items = count_files(directory)
        if total_items > 1:
            print("¦¹¥Ø¿ý¤Uªº¶µ¼ÆÁ`­p¤j©ó 2¡C")
            print(total_items)
            # Ū¨ú·Ó¤ù
            img1 = cv2.imread('C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST1.jpg')
            #½Æ»s·Ó¤ùÁקK´Ý¼v²{¶H None   
            img1 = img1.copy()  
        else:
            print("¦¹¥Ø¿ý¤Uªº¶µ¼ÆÁ`­p¤£¨¬ 2¡C")
            img1 = img.copy()

À³¸Ó¬O³o¤@¬q³y¦¨ªº¥d¹y ¤£ª¾¹D ¦³¨S¦³¤j¤j¥i¥H§iª¾¤p§Ì
¸Ó¦p¦ó­×§ï¤~¤£·|¥d¹y
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import cv2 #OpenCV version: 4.5.3  print("OpenCV version:", cv2.__version__)
  4. # import numpy as np
  5. import math
  6. import os

  7. # ¦C¥X«ü©w¥Ø¿ý¤¤ªº©Ò¦³ÀɮשM¤l¥Ø¿ý¡C
  8. def count_files(directory):
  9.     total = 0
  10.     for root, dirs, files in os.walk(directory):
  11.         total += len(files) + len(dirs)
  12.     return total

  13. # §R°£ªº¹Ï¹³
  14. def delete_image(directory, filename):
  15.     filepath = os.path.join(directory, filename)
  16.     if os.path.isfile(filepath):
  17.         os.remove(filepath)
  18.         print(f"Deleted {filename}.")
  19.     else:
  20.         print(f"{filename} ¤£¦s¦b")

  21. # ·í¥ªÁä«ö¤U¨Ã²¾°Ê®Éø»s¹Ï§Î¡Aevent¥i¥H¬d¬Ý²¾°Ê¡Aflag¬d¬Ý¬O§_«ö¤U
  22. # ©w¸q `draw_line()` ¨ç¼Æ¨Ó³B²z·Æ¹«¨Æ¥ó¡A¨Ò¦p¦b¹Ï¹³¤Wø»s½u±ø
  23. def draw_line(event, x, y, flags, param):
  24.     if event == cv2.EVENT_FLAG_LBUTTON:  # ¥ªÁä«ö¤U
  25.         global start_point, isDrawing ,img1

  26.         start_point = (x, y) # ©ì¦²·Æ¹«®É¡A¤£Â_°O¿ý®y¼Ð
  27.         isDrawing = True
  28.         print("¥ªÁä«ö¤U")
  29.         print(start_point)

  30.     elif event == cv2.EVENT_MOUSEMOVE and isDrawing:  # ²¾°Ê·Æ¹«®Éø¹Ï

  31.         # end_point = (x,y) # ©ì¦²·Æ¹«®É¡A¤£Â_°O¿ý®y¼Ð
  32.         end_point = (x,y)
  33.         print("²¾°Ê·Æ¹«")
  34.         print(end_point)

  35.         directory = 'C:/VSC_PY/GUI_AI/sand_train/text_dir/'  # ¥Ø¿ý¸ô®|
  36.         total_items = count_files(directory)
  37.         if total_items > 1:
  38.             print("¦¹¥Ø¿ý¤Uªº¶µ¼ÆÁ`­p¤j©ó 2¡C")
  39.             print(total_items)
  40.             # Ū¨ú·Ó¤ù
  41.             img1 = cv2.imread('C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST1.jpg')
  42.             #½Æ»s·Ó¤ùÁקK´Ý¼v²{¶H None   
  43.             img1 = img1.copy()  
  44.         else:
  45.             print("¦¹¥Ø¿ý¤Uªº¶µ¼ÆÁ`­p¤£¨¬ 2¡C")
  46.             img1 = img.copy()
  47.             

  48.         # P1 »P P2 ¬O¤wª¾ªºÂI
  49.         # P1 = (start_point[0], start_point[1])
  50.         # P2 = (end_point[0], end_point[1])  
  51.         P1 = (start_point[-2], start_point[-1])
  52.         P2 = (end_point[-2], end_point[-1])  
  53.         cv2.line(img1, P1, P2, (0, 255, 0), 3)

  54.         # ­pºâª½½uªø«×¤½¦¡¬°¡Glength = sqrt((x2-x1)^2 + (y2-y1)^2)
  55.         length = math.sqrt((P2[0] - P1[0])**2 + (P2[1] - P1[1])**2)
  56.         # length = np.sqrt((P2[0]-P1[0])**2 + (P2[1]-P1[1])**2) # ­pºâª½½uªºªø«×
  57.         # ªø«×³æ¦ì micrometer¡]um¡^
  58.         length = length * 1000

  59.         # Åã¥Ü®y¼Ð  cv2.threshold() strXY=str(x)+','+str(y)+"um"
  60.         strXY=str(x)+','+str(y)+','+str(length)+"um"
  61.         font=cv2.FONT_HERSHEY_PLAIN
  62.         cv2.putText(img1,strXY,(x+10,y+10),font,1,(0,255,0),2)

  63.         cv2.imshow("Image", img1)

  64.     elif event == cv2.EVENT_LBUTTONUP:  # ¥ªÁäÄÀ©ñ True False
  65.         isDrawing = False
  66.         print("¥ªÁäÄÀ©ñ")
  67.         print(isDrawing)
  68.         #«O¦s·Ó¤ù
  69.         cv2.imwrite('C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST1.jpg', img1)
  70.    
  71.     elif event == cv2.EVENT_RBUTTONDOWN:
  72.         print("¥kÁä«ö¤U")

  73.         if __name__ == "__main__":
  74.             directory = 'C:/VSC_PY/GUI_AI/sand_train/text_dir/'  # ¥Ø¿ý¸ô®|
  75.             filename = "TEST1.jpg" # ¹Ï¤ùªº¦WºÙ
  76.             delete_image(directory, filename) #§R°£·Ó¤ù
  77.         img3 = img.copy()
  78.         cv2.imshow("Image", img3)     

  79. # ¸ü¤J¹Ï¤ù 'C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST.jpg'
  80. # ¹w³]¨Ï¥Î cv2.IMREAD_COLOR ¼Ò¦¡
  81. img = cv2.imread('C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST.jpg')

  82. # cv2.imshow('oxxostudio', img) # ¨Ï¥Î¦W¬° oxxostudio ªºµøµ¡¶}±Ò¹Ï¤ù

  83. # «Ø¥ß¤@­Óµøµ¡¥HÅã¥Ü¼v¹³¨Ã³]©w¨Æ¥óºÊÅ¥¾¹
  84. cv2.namedWindow("Image")
  85. # °»´ú«ü©wµøµ¡¤Uªº·Æ¹«¨Æ¥ó
  86. cv2.setMouseCallback("Image", draw_line)

  87. # ±Ò°Ê¥D°j°é¥HÅã¥Ü©M§ó·s¹Ï¹³¡A¥]¬Aø»sªº½u±ø
  88. while True:
  89.     cv2.imshow("Image", img)      
  90.     # «ö¤U¥ô·NÁä°±¤î
  91.     if cv2.waitKey(0):
  92.         cv2.destroyAllWindows() # µ²§ô©Ò¦³¹Ï¤ùµøµ¡
  93.         break
½Æ»s¥N½X

        ÀR«ä¦Û¦b : ¡i®É¶¡¦pÆp¥Û¡j®É¶¡¹ï¤@­Ó¦³´¼¼zªº¤H¦Ó¨¥¡A´N¦pÆp¥Û¯ë¬Ã¶Q¡F¦ý¹ï·M¤H¨Ó»¡¡A«o¹³¬O¤@§âªd¤g¡A¤@ÂI»ù­È¤]¨S¦³¡C
ªð¦^¦Cªí ¤W¤@¥DÃD