如何编码以更改支持 Python OpenCV 硬币分割以支持框分割并在分割前将文件 Select 转换为 select 视频?

How to coding to change support Python OpenCV Coin Segmentation to support Box Segmentation and have File Select to select video before Segmentation?

如何编码以更改支持 Python OpenCV Circle (Coin) Segmentation 以支持 Square (Box) Segmentation 并在分割之前将文件 Select 转换为 select 视频?

我学习硬币分割:Python OpenCV 文章与文章 link https://medium.com/@kongruksiamza/coin-secmentation-python-opencv-9c7a9537002c and Source Code link https://github.com/kongruksiamza/Coin-Segmentation .

我在计算分割时遇到问题,只有圆(硬币)分割(不能计算方形(框)分割),分割前没有文件 select 到 select 视频。

示例代码。

  1. CoinDetection.py
import cv2
import numpy as np
cap=cv2.VideoCapture("Coin2.mp4")

while(cap.read()) :
     ref,frame = cap.read()
     roi=frame[:1080,0:1920]

     gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
     gray_blur=cv2.GaussianBlur(gray,(15,15),0)
     thresh=cv2.adaptiveThreshold(gray_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,1)
     kernel=np.ones((3,3),np.uint8)
     closing=cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=4)

     result_img=closing.copy()
     contours,hierachy=cv2.findContours(result_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
     counter=0
     for cnt in contours:
         area = cv2.contourArea(cnt)
         if area<5000 or area > 35000:
             continue
         ellipse = cv2.fitEllipse(cnt)
         cv2.ellipse(roi,ellipse,(0,255,0),2)
         counter+=1
    
     cv2.putText(roi,str(counter),(10,100),cv2.FONT_HERSHEY_SIMPLEX,4,(255,0,0),2,cv2.LINE_AA)
     cv2.imshow("Show",roi)

     if cv2.waitKey(1) & 0xFF==ord('q'):
         break

cap.release()
cv2.destroyAllWindows()
  1. 具有 link.
  2. 计数正方形(框)分割的示例图像

https://inwfile.com/s-fn/uzr1at.png

  1. UI 在分段之前将文件 Select 转换为 select 视频的示例。

https://i.imgur.com/pKtRoxj.png

Update :我添加代码以在使用我的完整源代码进行分段之前将文件 Select 添加到 select 视频。

我有 link1 , link2 , link3 and link4 的答案。

  1. 使用命令安装 pyinstaller / auto-py-to-exe / win32com :
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
pip install auto-py-to-exe
pip install pywin32
  1. 使用示例编码。

2.1 BoxDetection.py

import cv2
import numpy as np

from tkinter import Tk
from tkinter.filedialog import askopenfilename

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
cap=cv2.VideoCapture(filename) # Compatible with box2 mp4 video file

while(cap.read()) :
     ref,frame = cap.read()
     roi=frame[:1080,0:1920]

     gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
     gray_blur=cv2.GaussianBlur(gray,(25,25),0)
     thresh=cv2.adaptiveThreshold(gray_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,21,4)
     kernel=np.ones((3,3),np.uint8)
     closing=cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=4)

     result_img=closing.copy()
     contours,hierachy=cv2.findContours(result_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
     counter=0
     for cnt in contours:
         area = cv2.contourArea(cnt)
         if area<800 :
             continue
         # ellipse = cv2.fitEllipse(cnt)
         # cv2.ellipse(roi,ellipse,(0,255,0),2)
         counter+=1

     cv2.putText(roi,str(counter),(10,100),cv2.FONT_HERSHEY_SIMPLEX,4,(0,0,255),4,cv2.LINE_AA)
     cv2.imshow("Show",roi)

     if cv2.waitKey(300000) & 0xFF==ord('q'):
         break

cap.release()
cv2.destroyAllWindows()

和 VDO box2.mp4 示例文件 link - https://doanga2007.github.io/box2.mp4

  1. 打开cmd并使用auto-py-to-exe命令打开auto-py-to-exe程序,打开python文件并按编译按钮将python文件编译为exe立即归档。

  2. BoxDetection.exe 和 link - https://drive.google.com/file/d/1tnnnDWRrg1NbPZ3hr9mhY-t_4zry21rB/view?usp=sharing