【计算机视觉】pycharm自制一个吉他扒谱软件(附源码)
- 构思起源
- 结果展示
- 吉他音频和图片附件可以从我的git上下载
- 源码
构思起源
作为一个深度吉他爱好者,拥有一个自制的扒谱软件真是梦寐以求的事情,以前扒谱还要手画六线谱,谱子用完就扔了,很麻烦,现在有了这个软件一切就简单了,话不多说,下面就来看看吧
结果展示
运用鼠标按在上面的黄点,可以发出相应的吉他的声音,同时在上面的吉他谱上显示出相应的数字
吉他音频和图片附件可以从我的git上下载
链接: link.
源码
import cv2
from PIL import ImageGrab#截图的库
from playsound import playsound
image=cv2.imread(u'D:\\PycharmProjects\\pythonProject\\Saved Pictures\\IMG_20220228_123846_edit_112632392600521.jpg',1)#读取吉他图片
a=[]
b=[]#创建鼠标事件
def Event_Lbuttondown(event,x,y,flags,param):#设置鼠标事件,将点到的位置放出相应的声音if event==cv2.EVENT_LBUTTONDOWN:xy="%d,%d"%(x,y)a.append(x)b.append(y)#ab数组中存放了鼠标点击的坐标cv2.imshow("image",image)#--------当存了50个单音以后自动截图并保存再当前目录下,然后更新吉他谱if len(a)>50:img=ImageGrab.grab(bbox=(100,70,1830,400))str = input("输入歌曲名字")img.save(str+'.jpg')a.clear()cv2.rectangle(image, (100, 50), (1400, 240),(255, 255, 255), cv2.FILLED)cv2.line(image, (100, 70), (1400, 70), (255, 0, 0), 2)cv2.line(image, (100, 100), (1400, 100), (255, 0, 0), 2)cv2.line(image, (100, 130), (1400, 130), (255, 0, 0), 2)cv2.line(image, (100, 160), (1400, 160), (255, 0, 0), 2)cv2.line(image, (100, 190), (1400, 190), (255, 0, 0), 2)cv2.line(image, (100, 220), (1400, 220), (255, 0, 0), 2)#----------将每个案件结合鼠标事件,因为吉他中每个按键不是均匀分布的,所以我是一个一个敲的,没有用到矩阵,后面的代码大多数都差不多#------------------0品if 1457-12<x<1457+12 and \447-12<y<447+12:#---------------------鼠标坐标在黄色点上时触发鼠标事件cv2.putText(image,'0',(100+25*len(a),80),cv2.FONT_HERSHEY_PLAIN,2,(0,255,0),2)#--------同时将相应的数字写入吉他谱中cv2.imshow("image", image)#-----更新画面playsound(r"D:\音频文件\0_6.mp3")#-----同时播放音频文件if 1459 - 12 < x < 1459 + 12 and \477 - 12 < y < 477 + 12:cv2.putText(image, '0', (100+ 25 * len(a), 110), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\0_5.mp3")if 1464 - 12 < x < 1464+ 12 and \513 - 12 < y < 513 + 12:cv2.putText(image, '0', (100 + 25 * len(a), 140), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\0_4.mp3")if 1466 - 12 < x < 1466 + 12 and \546 - 12 < y < 546 + 12:cv2.putText(image, '0', (100 + 25 * len(a), 170), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\0_3.mp3")if 1465 - 12 < x < 1465 + 12 and \582 - 12 < y < 582 + 12:cv2.putText(image, '0', (100 + 25 * len(a), 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\0_2.mp3")if 1469 - 12 < x < 1469 + 12 and \614 - 12 < y < 614 + 12:cv2.putText(image, '0', (100 + 25 * len(a), 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\0_1.mp3")#------------------------第一品if 1374-12<x<1374+12 and \444-12<y<444+12:cv2.putText(image,'1',(100+25*len(a),80),cv2.FONT_HERSHEY_PLAIN,2,(0,255,0),2)cv2.imshow("image", image)playsound(r"D:\音频文件\1_1.mp3")if 1379 - 12 < x < 1379 + 12 and \477 - 12 < y < 477 + 12:cv2.putText(image, '1', (100+ 25 * len(a), 110), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\1_2.mp3")if 1381 - 12 < x < 1381+ 12 and \513 - 12 < y < 513 + 12:cv2.putText(image, '1', (100 + 25 * len(a), 140), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\1_3.mp3")if 1383 - 12 < x < 1383 + 12 and \546 - 12 < y < 546 + 12:cv2.putText(image, '1', (100 + 25 * len(a), 170), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\1_4.mp3")if 1386 - 12 < x < 1386 + 12 and \582 - 12 < y < 582 + 12:cv2.putText(image, '1', (100 + 25 * len(a), 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\1_5.mp3")if 1387 - 12 < x < 1387 + 12 and \617 - 12 < y < 617 + 12:cv2.putText(image, '1', (100 + 25 * len(a), 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\1_6.mp3")#-------------第二品if 1275 - 12 < x < 1275 + 12 and \439 - 12 < y < 439 + 12:cv2.putText(image, '2', (100 + 25 * len(a), 80), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\2_1.mp3")if 1277 - 12 < x < 1277 + 12 and \474 - 12 < y < 474 + 12:cv2.putText(image, '2', (100 + 25 * len(a), 110), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\2_2.mp3")if 1280 - 12 < x < 1280 + 12 and \511 - 12 < y < 511 + 12:cv2.putText(image, '2', (100 + 25 * len(a), 140), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\2_3.mp3")if 1279 - 12 < x < 1279 + 12 and \545 - 12 < y < 545 + 12:cv2.putText(image, '2', (100 + 25 * len(a), 170), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\2_4.mp3")if 1282 - 12 < x < 1282 + 12 and \584 - 12 < y < 584 + 12:cv2.putText(image, '2', (100 + 25 * len(a), 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\2_5.mp3")if 1282 - 12 < x < 1282 + 12 and \619 - 12 < y < 619 + 12:cv2.putText(image, '2', (100 + 25 * len(a), 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\2_6.mp3")#--------------------------333if 1180 - 12 < x < 1180 + 12 and \436 - 12 < y < 436 + 12:cv2.putText(image, '3', (100 + 25 * len(a), 80), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\3_1.mp3")if 1184 - 12 < x < 1184 + 12 and \474 - 12 < y < 474 + 12:cv2.putText(image, '3', (100 + 25 * len(a), 110), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\3_2.mp3")if 1185 - 12 < x < 1185 + 12 and \511 - 12 < y < 511 + 12:cv2.putText(image, '3', (100 + 25 * len(a), 140), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\3_3.mp3")if 1184 - 12 < x < 1184 + 12 and \549 - 12 < y < 549 + 12:cv2.putText(image, '3', (100 + 25 * len(a), 170), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\3_4.mp3")if 1185 - 12 < x < 1185 + 12 and \584 - 12 < y < 584 + 12:cv2.putText(image, '3', (100 + 25 * len(a), 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\3_5.mp3")if 1187 - 12 < x < 1187 + 12 and \623 - 12 < y < 623 + 12:cv2.putText(image, '3', (100 + 25 * len(a), 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\3_6.mp3")#--------------------4if 1086 - 12 < x < 1086 + 12 and \433 - 12 < y < 433 + 12:cv2.putText(image, '4', (100 + 25 * len(a), 80), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\4_1.mp3")if 1092 - 12 < x < 1092 + 12 and \471 - 12 < y < 471 + 12:cv2.putText(image, '4', (100 + 25 * len(a), 110), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\4_2.mp3")if 1095 - 12 < x < 1095 + 12 and \511 - 12 < y < 511 + 12:cv2.putText(image, '4', (100 + 25 * len(a), 140), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\4_3.mp3")if 1095 - 12 < x < 1095 + 12 and \549 - 12 < y < 549 + 12:cv2.putText(image, '4', (100 + 25 * len(a), 170), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\4_4.mp3")if 1095 - 12 < x < 1095 + 12 and \587 - 12 < y < 587 + 12:cv2.putText(image, '4', (100 + 25 * len(a), 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\4_5.mp3")if 1095 - 12 < x < 1095 + 12 and \627 - 12 < y < 627 + 12:cv2.putText(image, '4', (100 + 25 * len(a), 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\4_6.mp3")#---------------5if 1008-12<x<1008+12 and \430-12<y<430+12:cv2.putText(image,'5',(100+25*len(a),80),cv2.FONT_HERSHEY_PLAIN,2,(0,255,0),2)cv2.imshow("image", image)playsound(r"D:\音频文件\5_1.mp3")if 1006 - 12 < x < 1006 + 12 and \471 - 12 < y < 471 + 12:cv2.putText(image, '5', (100+ 25 * len(a), 110), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\5_2.mp3")if 1006 - 12 < x < 1006+ 12 and \511 - 12 < y < 511 + 12:cv2.putText(image, '5', (100 + 25 * len(a), 140), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\5_3.mp3")if 1010 - 12 < x < 1010 + 12 and \549 - 12 < y < 549 + 12:cv2.putText(image, '5', (100 + 25 * len(a), 170), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\5_4.mp3")if 1010 - 12 < x < 1010 + 12 and \589 - 12 < y < 589 + 12:cv2.putText(image, '5', (100 + 25 * len(a), 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\5_5.mp3")if 1010 - 12 < x < 1010 + 12 and \629 - 12 < y < 629 + 12:cv2.putText(image, '5', (100 + 25 * len(a), 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\5_6.mp3")#-----------------6666if 921-12<x<921+12 and \426-12<y<426+12:cv2.putText(image,'6',(100+25*len(a),80),cv2.FONT_HERSHEY_PLAIN,2,(0,255,0),2)cv2.imshow("image", image)playsound(r"D:\音频文件\6_1.mp3")if 922 - 12 < x < 922 + 12 and \471 - 12 < y < 471 + 12:cv2.putText(image, '6', (100+ 25 * len(a), 110), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\6_2.mp3")if 921 - 12 < x < 921 + 12 and \511 - 12 < y < 511 + 12:cv2.putText(image, '6', (100 + 25 * len(a), 140), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\6_3.mp3")if 919 - 12 < x < 919 + 12 and \551 - 12 < y < 551 + 12:cv2.putText(image, '6', (100 + 25 * len(a), 170), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\6_4.mp3")if 923 - 12 < x < 923 + 12 and \592 - 12 < y < 592 + 12:cv2.putText(image, '6', (100 + 25 * len(a), 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\6_5.mp3")if 920 - 12 < x < 920 + 12 and \637 - 12 < y < 637 + 12:cv2.putText(image, '6', (100 + 25 * len(a), 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\6_6.mp3")#-----------------77if 842-12<x<842+12 and \425-12<y<425+12:cv2.putText(image,'7',(100+25*len(a),80),cv2.FONT_HERSHEY_PLAIN,2,(0,255,0),2)cv2.imshow("image", image)playsound(r"D:\音频文件\7_1.mp3")if 842 - 12 < x < 842 + 12 and \470 - 12 < y < 470 + 12:cv2.putText(image, '7', (100+ 25 * len(a), 110), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\7_2.mp3")if 846 - 12 < x < 846 + 12 and \507 - 12 < y < 507 + 12:cv2.putText(image, '7', (100 + 25 * len(a), 140), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\7_3.mp3")if 845 - 12 < x < 845 + 12 and \552 - 12 < y < 552 + 12:cv2.putText(image, '7', (100 + 25 * len(a), 170), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\7_4.mp3")if 846 - 12 < x < 846 + 12 and \595 - 12 < y < 595 + 12:cv2.putText(image, '7', (100 + 25 * len(a), 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\7_5.mp3")if 846 - 12 < x < 846 + 12 and \639 - 12 < y < 639 + 12:cv2.putText(image, '7', (100 + 25 * len(a), 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)cv2.imshow("image", image)playsound(r"D:\音频文件\7_6.mp3")image=cv2.resize(image,(1500,700))#将图片拉伸成相应的大小#画出吉他谱cv2.rectangle(image, (100,50), (1400,240),(255,255,255), cv2.FILLED)
cv2.line(image,(100,70),(1400,70),(255,0,0),2)
cv2.line(image,(100,100),(1400,100),(255,0,0),2)
cv2.line(image,(100,130),(1400,130),(255,0,0),2)
cv2.line(image,(100,160),(1400,160),(255,0,0),2)
cv2.line(image,(100,190),(1400,190),(255,0,0),2)
cv2.line(image,(100,220),(1400,220),(255,0,0),2)#将吉他上的所有建用黄点标出cv2.putText(image,"0",(1453,383),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255),5)
cv2.circle(image,(1457,447),12,(0,255,255),-1)
cv2.circle(image,(1459 ,478),12,(0,255,255),-1)
cv2.circle(image,(1464 ,514),12,(0,255,255),-1)
cv2.circle(image,(1466 ,546),12,(0,255,255),-1)
cv2.circle(image,(1465 ,581),12,(0,255,255),-1)
cv2.circle(image,(1469 ,614),12,(0,255,255),-1)cv2.putText(image,"1",(1373,389),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255),5)
cv2.circle(image,(1374,444),12,(0,255,255),-1)
cv2.circle(image,(1379 ,477),12,(0,255,255),-1)
cv2.circle(image,(1381 ,513),12,(0,255,255),-1)
cv2.circle(image,(1383 ,546),12,(0,255,255),-1)
cv2.circle(image,(1386 ,582),12,(0,255,255),-1)
cv2.circle(image,(1387 ,617),12,(0,255,255),-1)cv2.putText(image,"2",(1277,383),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255),5)
cv2.circle(image,(1275,439),12,(0,255,255),-1)
cv2.circle(image,(1277 ,474),12,(0,255,255),-1)
cv2.circle(image,(1280 ,511),12,(0,255,255),-1)
cv2.circle(image,(1279 ,545),12,(0,255,255),-1)
cv2.circle(image,(1282 ,584),12,(0,255,255),-1)
cv2.circle(image,(1282 ,619),12,(0,255,255),-1)cv2.putText(image,"3",(1181,379),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255),5)
cv2.circle(image,(1180,436),12,(0,255,255),-1)
cv2.circle(image,(1184 ,473),12,(0,255,255),-1)
cv2.circle(image,(1185 ,511),12,(0,255,255),-1)
cv2.circle(image,(1184 ,549),12,(0,255,255),-1)
cv2.circle(image,(1185 ,585),12,(0,255,255),-1)
cv2.circle(image,(1187 ,623),12,(0,255,255),-1)cv2.putText(image,"4",(1097,369),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255),5)
cv2.circle(image,(1086,433),12,(0,255,255),-1)
cv2.circle(image,(1092 ,471),12,(0,255,255),-1)
cv2.circle(image,(1095 ,511),12,(0,255,255),-1)
cv2.circle(image,(1095 ,549),12,(0,255,255),-1)
cv2.circle(image,(1095 ,587),12,(0,255,255),-1)
cv2.circle(image,(1095 ,627),12,(0,255,255),-1)cv2.putText(image,"5",(1013,364),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255),5)
cv2.circle(image,(1008,430),12,(0,255,255),-1)
cv2.circle(image,(1006 ,471),12,(0,255,255),-1)
cv2.circle(image,(1006 ,511),12,(0,255,255),-1)
cv2.circle(image, (1010 ,549),12,(0,255,255),-1)
cv2.circle(image,(1010 ,589),12,(0,255,255),-1)
cv2.circle(image,(1009 ,629),12,(0,255,255),-1)cv2.putText(image,"6",(932,359),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255),5)
cv2.circle(image,(921,426),12,(0,255,255),-1)
cv2.circle(image,(922 ,471),12,(0,255,255),-1)
cv2.circle(image,(921 ,511),12,(0,255,255),-1)
cv2.circle(image,(919 ,551),12,(0,255,255),-1)
cv2.circle(image,(923 ,592),12,(0,255,255),-1)
cv2.circle(image,(920 ,637),12,(0,255,255),-1)cv2.putText(image,"7",(852,354),cv2.FONT_HERSHEY_PLAIN,3,(0,0,255),5)
cv2.circle(image,(842,425),12,(0,255,255),-1)
cv2.circle(image,(842,470),12,(0,255,255),-1)
cv2.circle(image,(846 ,507),12,(0,255,255),-1)
cv2.circle(image,(845 ,552),12,(0,255,255),-1)
cv2.circle(image,(846 ,595),12,(0,255,255),-1)
cv2.circle(image,(846 ,639),12,(0,255,255),-1)cv2.namedWindow('image')
cv2.setMouseCallback("image",Event_Lbuttondown)#相应鼠标事件
cv2.imshow("image",image)#显示图片,将矩阵空间变为图片cv2.waitKey(0)#等待键盘操作执行相应操作
cv2.destroyWindow()