如何解决检测/识别如何工作Python?
我正在编写一个日文绘图应用程序,您必须在其中绘制您在左上角看到的内容。 screen image 我现在的问题是我想让它识别我绘制的形状并将其与所有原件进行比较,然后告诉我最相似的图片是什么。
我的代码:
import pygame
import random
from button import Button
import how_similar_two_images_are
pygame.init()
# Variables (window_size,color)
width = 600
height = 400
white = (255,255,255)
black = (0,0)
# Window Surface
window = pygame.display.set_mode([width,height])
window_name = pygame.display.set_caption("Drawing Game!")
window.fill(white)
clock = pygame.time.Clock()
done = False
pressed = False
thickness = 5
line_list = [None]
hiragana = ["a","i","u","e","o","ka","ki","ku","ke","ko","sa","shi","ta","chi","tsu","te","to","na","ni","nu","ne","no","ha","hi","fu","he","ho","n","wa","wo","yo","yu","ya"]
# tile size 116x90
state = False
silimar = how_similar_two_images_are.How_similar()
def show_hira():
# 0
rng = random.randint(0,len(hiragana)-1)
image = pygame.image.load(f".\\Projekte\\Japanese Learning App\\img\\{hiragana[rng]}.png")
window.blit(image,[0,29,22.5])
def show_letters():
# 1
rng = random.randint(0,len(hiragana)-1)
window.blit(pygame.font.SysFont("Arial",40).render(hiragana[rng],False,black),[10,10])
show_hira()
# Main loop for the game
while not done:
try:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
window.fill(white)
if state:
show_letters()
else: show_hira()
if event.key == pygame.K_UP:
pygame.image.save(window,".\\Projekte\\Japanese Learning App\\my_try.png","png")
print("Screen wurde gespeichert!")
if event.key == pygame.K_h:
window.fill(white)
if event.key == pygame.K_s:
if state:
state = False
else: state = True
if event.type == pygame.MOUSEBUTTONDOWN: pressed = True
if event.type != pygame.MOUSEBUTTONDOWN: pressed = False
# line logic
line_list.append(pygame.mouse.get_pos())
item1 = line_list[0]
item2 = line_list[1]
# drawing with mouse pressed
if pygame.mouse.get_pressed() == (1,0) and item1 != item2:
pygame.draw.line(window,black,item1,item2,thickness)
if pygame.mouse.get_pressed() == (0,1) and item1 != item2:
pygame.draw.line(window,white,thickness)
line_list.pop(0)
# displays the drawing on the screen
pygame.display.update()
# framerate
clock.tick(1000000)
except Exception as e:
print(e)
pygame.quit()
done = True
pygame.quit()
我尝试了什么:
我尝试了网络上的所有 OpenCV 源代码,但没有任何效果。这是我得到的最好的结果,但它对我来说仍然不够好。
OpenCV 代码(尝试):
import cv2
import numpy as np
import glob
all_images_to_compare = []
titles = []
temp = 0
best_comparision = ""
best_comparision_img = None
_file = "\\Projekte\\OpenCV\\detect\\images\\*"
for f in glob.iglob(_file):
image = cv2.imread(f)
titles.append(f)
all_images_to_compare.append(image)
original = cv2.imread("\\Projekte\\OpenCV\\detect\\my_try.png")
original = cv2.resize(original,(116,90),interpolation=cv2.INTER_AREA)
for image_to_compare,titles in zip(all_images_to_compare,titles):
# 1) Check if 2 images are equals
if original.shape == image_to_compare.shape:
print("The images have same size and channels")
difference = cv2.subtract(original,image_to_compare)
b,g,r = cv2.split(difference)
if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
print("The images are completely Equal")
else:
print("The images are NOT equal")
# 2) Check for similarities between the 2 images
sift = cv2.xfeatures2d.SIFT_create()
kp_1,desc_1 = sift.detectAndCompute(original,None)
kp_2,desc_2 = sift.detectAndCompute(image_to_compare,None)
index_params = dict(algorithm=0,trees=5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(desc_1,desc_2,k=2)
good_points = []
for m,n in matches:
if m.distance < 0.6*n.distance:
good_points.append(m)
# Define how similar they are
number_keypoints = 0
if len(kp_1) <= len(kp_2):
number_keypoints = len(kp_1)
else:
number_keypoints = len(kp_2)
if len(good_points) / number_keypoints * 100 > temp:
best_comparision = titles
# best_comparision_img = cv2.imread(titles)
temp = len(good_points)/ number_keypoints * 100
print("Keypoints 1ST Image: " + str(len(kp_1)))
print("Keypoints 2ND Image: " + str(len(kp_2)))
print("GOOD Matches:",len(good_points))
try:
print("How good it's the match: ",len(good_points) / number_keypoints * 100)
except Exception as e:
print(e)
print("The best result is " + best_comparision)
result = cv2.drawMatches(original,kp_1,image_to_compare,kp_2,good_points,None)
cv2.imshow("result",cv2.resize(result,(116*7,90*6),fx=0.4,fy=0.4))
cv2.imwrite("./feature_matching.jpg",result)
cv2.waitKey(0)
cv2.destroyAllWindows()
我愿意接受任何建议。我只希望它最终完成。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。