对图片进行索引,存入数据库sqlite3中,实现快速搜索打开

时间:2021-07-13 01:48:09

对图片进行索引,存入数据库中,实现快速搜索打开

对图片进行索引,存入数据库sqlite3中,实现快速搜索打开  对图片进行索引,存入数据库sqlite3中,实现快速搜索打开

 

这个任务分为两步:

对图片进行索引,存入数据库sqlite3中,实现快速搜索打开

第一步:建立索引

 1 import os
 2 import shutil
 3 import sqlite3
 4 # 扫描函数,需扫描路径目录处理该目录及下级目录所有图片地址,简单明了
 5 
 6 def scanDirSearchFile(dir, dbname):
 7     connsqlite3 = sqlite3.connect(dbname)
 8     cu = connsqlite3.cursor()
 9     for root, dirs, files in os.walk(dir, True, None, False):  # 遍列目录
10         # 处理该文件夹下所有文件:
11 
12         for f in files:
13             if os.path.isfile(os.path.join(root, f)):
14                 ext = os.path.splitext(f)[1].lower()
15                 if ext in ('.jpg', '.png', '.bmp'):
16                     # print os.path.join(root,f)
17                     cu.execute("insert into photoaddress(photoaddress,photoname) values(?,?);",
18                                ((os.path.join(root, f)), (f)))
19                     pass
20     connsqlite3.commit()
21 # 初始化数据库表
22 def CreatePhotoDB(sqliteName):
23    # 数据库文件处理
24     connsqlite3 = sqlite3.connect(sqliteName)
25     cu = connsqlite3.cursor()
26     # 创建表
27     sql = "create table IF NOT EXISTS photoaddress(photoaddress varchar(400),photoname varchar(200));"
28     cu.execute(sql)
29     # 创建图片地址索引
30     sql = "create index IF NOT EXISTS photoaddress_photoaddress  on  photoaddress(photoaddress);"
31     cu.execute(sql)
32     # 创建图片名索引
33     sql = "create index IF NOT EXISTS photoaddress_photoname  on  photoaddress(photoname);"
34     cu.execute(sql)
35     # 清空库表
36     sql = "delete from photoaddress;"
37     cu.execute(sql)
38     connsqlite3.commit()
39 if __name__ == "__main__":
40     path = 'E:\\Users\A\Pictures\Pictures\lfw'
41     work_dir = os.path.abspath(path)  # 图片放置目录
42 
43     sqliteName = os.path.join(work_dir, "photo.db")  # sqlite数据库文件
44 
45     # 初始化数据库表
46     CreatePhotoDB(sqliteName)
47 
48     # 扫描目录,刷新数据库
49 
50 
51     scanDirSearchFile(work_dir, sqliteName)

第二部:界面+索引比对:

  1 import sys
  2 import pandas as pd
  3 from PIL import Image
  4 import matplotlib.pyplot as plt
  5 import sqlite3
  6 from PyQt5 import QtWidgets, QtCore
  7 from PyQt5.QtGui import  *
  8 from PyQt5.QtWidgets import *
  9 from PyQt5.QtCore import*
 10 from PyQt5 import QtCore
 11 dbpath = 'E:\\Users\A\Pictures\Pictures\lfw\\photo.db'
 12 conn = sqlite3.connect(dbpath)
 13 class MainWindow(QWidget):
 14     def __init__(self,parent=None):
 15         super(MainWindow,self).__init__(parent)
 16         self.status = 0
 17         self.initUi(MainWindow)
 18         self.layout = QGridLayout(self)
 19         self.setLayout(self.layout)
 20 
 21     def initUi(self,MainWindow):
 22         self.createGridGroupBox()
 23         self.creatFormGroupBox()
 24 
 25 
 26         mainLayout = QVBoxLayout()
 27         hboxLayout = QHBoxLayout()
 28         # hboxLayout.addStretch()/拉升上面空间
 29         hboxLayout.addWidget(self.gridGroupBox)
 30         mainLayout.addLayout(hboxLayout)
 31         mainLayout.addWidget(self.formGroupBox)
 32         self.setLayout(mainLayout)
 33 
 34     @pyqtSlot(bool)
 35     def on_btn_open_clicked(self):
 36         self.status = 1
 37         self.filename = QFileDialog.getOpenFileName(self, "OpenFile", ".",
 38                                                     "Image Files(*.jpg *.jpeg *.png)")[0]
 39         # filename = self.filename
 40         print(self.filename)
 41         print(type(self.filename))
 42         print('status:',self.status)
 43         self.createGridGroupBox()
 44         self.picture_R(self.filename)
 45         # self.initUi()
 46         # print(self.createGridGroupBox(filename))
 47 
 48         return self.status,self.filename
 49 
 50 
 51     def createGridGroupBox(self):
 52         self.gridGroupBox = QGroupBox("Demo")
 53         layout = QGridLayout()
 54         nameLabel = QLabel("picture_name")
 55         self.nameLineEdit = QLineEdit("")
 56         layout.addWidget(nameLabel, 1, 0)
 57         layout.addWidget(self.nameLineEdit, 1, 1)
 58 
 59         imgeLabe1 = QLabel()
 60         imgeLabe2 = QLabel()
 61         # while(true):
 62         pixMap = None
 63 
 64 
 65         layout.setSpacing(10)
 66         layout.addWidget(imgeLabe1, 0, 2, 4, 1)
 67         layout.update()
 68         self.gridGroupBox.setLayout(layout)
 69         self.setWindowTitle('Search picture')
 70 
 71 
 72     def creatFormGroupBox(self):
 73         self.formGroupBox = QGroupBox("Form layout")
 74         layout = QFormLayout()
 75 
 76         serchBtn = QPushButton('查找')
 77 
 78         # manage_Btn.clicked.connect(manage_ui.manage)
 79         serchBtn.clicked.connect(self.name_link)
 80         # tphoBtn.clicked.connect(realtime_facenet_git.detectface)
 81 
 82 
 83         imgeLabe2 = QLabel()
 84         pixMap = QPixmap("fileName1")
 85         imgeLabe2.setPixmap(pixMap)
 86         layout.addWidget(imgeLabe2)
 87         hbox = QHBoxLayout()
 88         hbox.addWidget(serchBtn)
 89 
 90         vbox = QVBoxLayout()
 91         vbox.addLayout(hbox)
 92 
 93         self.formGroupBox.setLayout(vbox)
 94     def getName(self):
 95         name = self.nameLineEdit.text()  # 获取文本框内容
 96 
 97         df = ('文件名: %s \n ' % (name))
 98         print(df)
 99         return name
100     def name_link(self):
101 
102         cursor = conn.execute("SELECT photoname,photoaddress from photoaddress")
103         filename = self.getName()
104         # filename = "202599.jpg"
105         for row in cursor:
106             # print(row[0])
107             if row[0] == filename:
108                 print(row[1])
109 
110                 img = Image.open(row[1])
111                 plt.figure("Results")
112                 plt.imshow(img)
113                 plt.show()
114 if __name__ == '__main__':
115     app = QApplication(sys.argv)
116     ex = MainWindow()
117     ex.show()
118     sys.exit(app.exec_())

对图片进行索引,存入数据库sqlite3中,实现快速搜索打开