Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as gp
- import os
- import sys
- import pathlib
- import datetime
- import time
- from mysql.connector import (connection)
- datasetName = ""
- path = "/mnt/storage/ddr/DDR"
- tags = []
- imagesNames = []
- gp.setwarnings(False)
- gp.setmode(gp.BOARD)
- gp.setup(7, gp.OUT)
- gp.setup(11, gp.OUT)
- gp.setup(12, gp.OUT)
- gp.setup(15, gp.OUT)
- gp.setup(16, gp.OUT)
- gp.setup(21, gp.OUT)
- gp.setup(22, gp.OUT)
- def cleanUp():
- gp.output(7, False)
- gp.output(11, False)
- gp.output(12, True)
- def selectCamOne():
- gp.output(7, False)
- gp.output(11, False)
- gp.output(12, True)
- def capture(imagesDir ,name,):
- ts = time.time()
- ts = int(ts)
- imageName = "%s_%s.jpg" % (str(name), str(ts))
- path = os.path.join(imagesDir, imageName)
- cmd = "raspistill -sh 100 -sa 90 -o " + path
- done = os.system(cmd)
- return imageName
- def takeSnapshot(cam, name):
- selectCamOne()
- return capture(cam, name)
- def createTagsFile(fullPath, name ,tags):
- path = os.path.dirname(fullPath)
- filename = os.path.join(path, '{}_tags.txt'.format(name))
- with open(filename, mode='wt', encoding='utf-8') as myfile:
- myfile.write('\n'.join(tags))
- def createImagesFile(fullPath, name, images):
- path = os.path.dirname(fullPath)
- filename = os.path.join(path, '{}_images.txt'.format(name))
- if os.path.exists(filename):
- mode = 'a'
- else:
- mode = 'w'
- with open(filename, mode=mode, encoding='utf-8') as myfile:
- for image in images:
- imagePath = os.path.join(fullPath, image)
- myfile.write(imagePath + '\n')
- def createFolders(path, datasetFolder):
- now = datetime.datetime.now()
- currDate = now.strftime("%Y_%m_%d")
- datasetPath = os.path.join(path, datasetFolder, currDate)
- pathlib.Path(datasetPath).mkdir(parents=True, exist_ok=True)
- return datasetPath
- def addTagsIfNotExists(cursor, tags):
- query = "INSERT IGNORE INTO tags (name) VALUES ('{}')"
- for tag in tags:
- cursor.execute(query.format(tag))
- def addDatasetIfNotExists(cursor, setName):
- query = "INSERT IGNORE INTO sets (name) VALUES ('{}')".format(setName)
- cursor.execute(query)
- def addTagsDatasetMap(cursor, setName, tags):
- query = "SELECT id FROM sets WHERE name = '{}'".format(setName)
- cursor.execute(query)
- id = cursor.fetchone()
- print(id)
- for tag in tags:
- tagIdQuery = "SELECT id FROM tags WHERE name = '{}'".format(tag)
- cursor.execute(tagIdQuery)
- tagId = cursor.fetchone()
- insertQuery = "INSERT IGNORE INTO sets_tags_map (set_id, tag_id) VALUES ({}, {})".format(id[0], tagId[0])
- cursor.execute(insertQuery)
- if __name__ == "__main__":
- cnx = connection.MySQLConnection(user='root', password='swnyF74W9b', host='192.168.1.153', database='datasets')
- mycursor = cnx.cursor()
- datasetName = input('podaj nazwę datasetu: ')
- tag = input("Wpisz 'q' aby zakończyć wpisywanie tagów. podaj taga: ")
- while tag != 'q':
- tags.append(tag)
- tag = input("Wpisz 'q' aby zakończyć wpisywanie tagów. podaj taga: ")
- print( "Czy chcesz utwożyć dataset {} w folderze {} z tagami: {} ?".format(datasetName, path, tags))
- create = input("Tak / Nie ?")
- if (create == "Tak" or create == "tak" or create == "t" or create == "T"):
- addTagsIfNotExists(mycursor, tags)
- addDatasetIfNotExists(mycursor, datasetName)
- addTagsDatasetMap(mycursor, datasetName, tags)
- cnx.commit()
- fullPath = createFolders(path, datasetName)
- createTagsFile(fullPath, datasetName, tags)
- action = input("Wpisz 'q' aby zakończyć, x aby zrobić zdjęcie ")
- while action != 'q':
- imagesNames.append( capture(fullPath, datasetName) )
- action = input("Wpisz 'q' aby zakończyć, x aby zrobić zdjęcie ")
- createImagesFile(fullPath, datasetName, imagesNames)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement