|
| 1 | +from PyQt5.QtGui import * |
| 2 | +from PyQt5.QtCore import * |
| 3 | +from PyQt5.QtWidgets import * |
| 4 | +from PyQt5.QtMultimedia import * |
| 5 | +from PyQt5.QtMultimediaWidgets import * |
| 6 | + |
| 7 | +import sys |
| 8 | +import time |
| 9 | +import pathlib |
| 10 | + |
| 11 | +BASE_DIR = pathlib.Path().home() |
| 12 | +FOLDER_NAME = 'Images' |
| 13 | + |
| 14 | +SAVE_PATH = BASE_DIR / FOLDER_NAME |
| 15 | +SAVE_PATH.mkdir(exist_ok=True, parents=True) |
| 16 | + |
| 17 | +class MainWindow(QMainWindow): |
| 18 | + def __init__(self): |
| 19 | + super().__init__() |
| 20 | + |
| 21 | + self.availableCameras = QCameraInfo.availableCameras() |
| 22 | + if not self.availableCameras: |
| 23 | + exit(0) |
| 24 | + self.status = QStatusBar() |
| 25 | + self.setStatusBar(self.status) |
| 26 | + |
| 27 | + self.savePath = None |
| 28 | + self.viewFinder = QCameraViewfinder() |
| 29 | + self.setCentralWidget(self.viewFinder) |
| 30 | + self.selectCamera(0) |
| 31 | + |
| 32 | + toolBar = QToolBar('Camera Tool Bar') |
| 33 | + toolBar.setIconSize(QSize(40, 40)) |
| 34 | + self.addToolBar(toolBar) |
| 35 | + |
| 36 | + clickAction = QAction(QIcon('Images/Capture.png'), 'Click Photo', self) |
| 37 | + clickAction.setStatusTip('This will Capture Pictures') |
| 38 | + clickAction.setToolTip('Capture Pictures') |
| 39 | + clickAction.triggered.connect(self.clickPhoto) |
| 40 | + toolBar.addAction(clickAction) |
| 41 | + |
| 42 | + changeFolderAction = QAction(QIcon('Images/Save.png'), 'Set Save Location', self) |
| 43 | + changeFolderAction.setStatusTip('This will Change the Save Location') |
| 44 | + changeFolderAction.setToolTip('Change Save Location') |
| 45 | + changeFolderAction.triggered.connect(self.changeFolderName) |
| 46 | + toolBar.addAction(changeFolderAction) |
| 47 | + |
| 48 | + cameraSelector = QComboBox() |
| 49 | + cameraSelector.setToolTip('Select Camera') |
| 50 | + cameraSelector.setStatusTip('Choose Camera to take Pictures') |
| 51 | + cameraSelector.setToolTipDuration(2500) |
| 52 | + cameraSelector.addItems([camera.description() |
| 53 | + for camera in self.availableCameras]) |
| 54 | + toolBar.addWidget(cameraSelector) |
| 55 | + |
| 56 | + def selectCamera(self, i): |
| 57 | + self.camera = QCamera(self.availableCameras[i]) |
| 58 | + self.camera.setViewfinder(self.viewFinder) |
| 59 | + self.camera.setCaptureMode(QCamera.CaptureStillImage) |
| 60 | + self.camera.error.connect(lambda: |
| 61 | + self.alert(self.camera.errorString())) |
| 62 | + self.camera.start() |
| 63 | + self.capture = QCameraImageCapture(self.camera) |
| 64 | + self.capture.error.connect(lambda d, i: |
| 65 | + self.status.showMessage( |
| 66 | + f'Image Captured {str(self.saveSeq)}')) |
| 67 | + self.currentCameraName = self.availableCameras[i].description() |
| 68 | + self.saveSeq = 0 |
| 69 | + |
| 70 | + def clickPhoto(self): |
| 71 | + timeStamp = time.strftime('Date %d %b %Y Time %H %M %S') |
| 72 | + fileName = f'Webcam {self.currentCameraName} {timeStamp} .jpg' |
| 73 | + if self.savePath: |
| 74 | + savePath = self.savePath / fileName |
| 75 | + else: |
| 76 | + savePath = SAVE_PATH / fileName |
| 77 | + self.saveImage(str(savePath)) |
| 78 | + print('Image saved on ', str(savePath)) |
| 79 | + self.saveSeq += 1 |
| 80 | + |
| 81 | + def changeFolderName(self): |
| 82 | + path = QFileDialog.getExistingDirectory(self, "Picture Location", "") |
| 83 | + mode = 0o666 |
| 84 | + path = pathlib.Path(path) |
| 85 | + self.savePath = path |
| 86 | + self.savePath.mkdir(exist_ok=True, parents=True, mode=mode) |
| 87 | + |
| 88 | + def saveImage(self, savePath:str): |
| 89 | + self.capture.capture(savePath) |
| 90 | + |
| 91 | + def alert(self, msg): |
| 92 | + error = QErrorMessage(self) |
| 93 | + error.showMessage(msg) |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + App = QApplication(sys.argv) |
| 98 | + App.setStyle('Fusion') |
| 99 | + window = MainWindow() |
| 100 | + window.setWindowTitle('Camera --by Arvind') |
| 101 | + window.setWindowIcon(QIcon('Images/Logo.png')) |
| 102 | + window.resize(1100, 700) |
| 103 | + window.show() |
| 104 | + sys.exit(App.exec()) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + main() |
0 commit comments