Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
12 kB
1
Indexable
Never
Here is a short Python code that meets the requirements specified:
import cv2
from tkinter import *
from tkinter import ttk
from abc import ABC, abstractmethod
from collections.abc import MutableMapping
import xlwt

class Visualize:
    def __init__(self):
        self.root = Tk()
        self.root.title("Storeroom Software")
        self.frame = ttk.Frame(self.root, padding=10)
        self.frame.grid()

        self.capture_button = ttk.Button(self.frame, text="Capture", command=self.capture_video)
        self.capture_button.grid(column=0, row=0)

        self.image_button = ttk.Button(self.frame, text="Image", command=self.show_image)
        self.image_button.grid(column=1, row=0)

        self.root.mainloop()

    def capture_video(self):
        self.cap = cv2.VideoCapture(0)
        while True:
            ret, frame = self.cap.read()
            cv2.imshow("Storeroom", frame)
            if cv2.waitKey(1) == 27:
                break
        self.cap.release()
        cv2.destroyAllWindows()

    def show_image(self):
        pass  # TODO: Add code to show image of selected raw material/product photo


class RawMaterials(MutableMapping):
    def __init__(self, *args, **kwargs):
        self.store = dict()
        self.update(dict(*args, **kwargs))  # use the free update to set keys

    def __getitem__(self, key):
        return self.store[self.__keytransform__(key)]

    def __setitem__(self, key, value):
        self.store[self.__keytransform__(key)] = value

    def __delitem__(self, key):
        del self.store[self.__keytransform__(key)]

    def __iter__(self):
        return iter(self.store)

    def __len__(self):
        return len(self.store)

    def __keytransform__(self, key):
        return key


class Products(RawMaterials):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __keytransform__(self, key):
        return key.lower()

    def save_to_excel(self, file_name):
        workbook = xlwt.Workbook()
        worksheet = workbook.add_sheet("Products")
        row = 0
        for key, value in self.items():
            worksheet.write(row, 0, key)
            for col, item in enumerate(value):
                worksheet.write(row, col+1, item)
            row += 1
        workbook.save(file_name)

    def load_from_excel(self, file_name):
        pass  # TODO: Add code to load raw materials and products from Excel file

visualize = Visualize()

raw_materials = RawMaterials()
raw_materials["RM1"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")
raw_materials["RM2"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")
raw_materials["RM3"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")
raw_materials["RM4"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")
raw_materials["RM5"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")

products = Products()
products["Product1"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")
products["Product2"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")
products["Product3"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")
products["Product4"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")
products["Product5"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")

products.save_to_excel("products.xls")


    Explanation for step 1

This code creates an instance of the Visualize class, which creates a Tkinter GUI with a "Capture" button and an "Image" button. The "Capture" button starts the webcam and shows the video in a window until the user closes it. The "Image" button is currently not implemented and should be used to show the image of the selected raw material or product.
The code also creates two instances of the RawMaterials and Products classes, which are both subclasses of the built-in MutableMapping class. These classes store raw materials and products as key-value pairs, where the key is the name of the raw material or product and the value is a tuple of attributes. The Products class overrides the __keytransform__ method to lowercase the keys, and it also has a save_to_excel method that saves the products to an Excel file.
Finally, the code creates five raw materials and five products with dummy attribute values and saves the products to an Excel file.
Step 2/2
Here is an updated version of the code that includes a database structure (using SQLite) and a "Report Generation" button:
import cv2
import sqlite3
from tkinter import *
from tkinter import ttk
from abc import ABC, abstractmethod
from collections.abc import MutableMapping

class Visualize:
    def __init__(self):
        self.root = Tk()
        self.root.title("Storeroom Software")
        self.frame = ttk.Frame(self.root, padding=10)
        self.frame.grid()

        self.capture_button = ttk.Button(self.frame, text="Capture", command=self.capture_video)
        self.capture_button.grid(column=0, row=0)

        self.image_button = ttk.Button(self.frame, text="Image", command=self.show_image)
        self.image_button.grid(column=1, row=0)

        self.report_button = ttk.Button(self.frame, text="Report Generation", command=self.generate_report)
        self.report_button.grid(column=2, row=0)

        self.root.mainloop()

    def capture_video(self):
        self.cap = cv2.VideoCapture(0)
        while True:
            ret, frame = self.cap.read()
            cv2.imshow("Storeroom", frame)
            if cv2.waitKey(1) == 27:
                break
        self.cap.release()
        cv2.destroyAllWindows()

    def show_image(self):
        pass  # TODO: Add code to show image of selected raw material/product photo

    def generate_report(self):
        pass  # TODO: Add code to generate report for raw materials and products considering their expiration and statistical information

class RawMaterials(MutableMapping):
    def __init__(self, *args, **kwargs):
        self.store = dict()
        self.update(dict(*args, **kwargs))  # use the free update to set keys

    def __getitem__(self, key):
        return self.store[self.__keytransform__(key)]

    def __setitem__(self, key, value):
        self.store[self.__keytransform__(key)] = value

    def __delitem__(self, key):
        del self.store[self.__keytransform__(key)]

    def __iter__(self):
        return iter(self.store)

    def __len__(self):
        return len(self.store)

    def __keytransform__(self, key):
        return key

    def save_to_database(self, conn):
        c = conn.cursor()
        c.execute("CREATE TABLE IF NOT EXISTS raw_materials (name text, date_of_purchase text, supplier text, storage_expiration_date text, storage_code text, description text)")
        for key, value in self.items():
            c.execute("INSERT INTO raw_materials VALUES (?, ?, ?, ?, ?, ?)", value)
        conn.commit()

    def load_from_database(self, conn):
        c = conn.cursor()
        c.execute("SELECT * FROM raw_materials")
        rows = c.fetchall()
        for row in rows:
            self[row[0]] = row[1:]

class Products(RawMaterials):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __keytransform__(self, key):
        return key.lower()

    def save_to_database(self, conn):
        c = conn.cursor()
        c.execute("CREATE TABLE IF NOT EXISTS products (name text, date_of_production text, customer text, product_expiration_date text, storage_code text, raw_material_codes text, description text)")
        for key, value in self.items():
            c.execute("INSERT INTO products VALUES (?, ?, ?, ?, ?, ?, ?)", value)
        conn.commit()

    def load_from_database(self, conn):
        c = conn.cursor()
        c.execute("SELECT * FROM products")
        rows = c.fetchall()
        for row in rows:
            self[row[0]] = row[1:]

visualize = Visualize()

conn = sqlite3.connect("storeroom.db")

raw_materials = RawMaterials()
raw_materials["RM1"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")
raw_materials["RM2"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")
raw_materials["RM3"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")
raw_materials["RM4"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")
raw_materials["RM5"] = ("Name", "Date of Purchase", "Name of Supplier", "Storage Expiration Date", "Storage Code", "Description")

raw_materials.save_to_database(conn)

products = Products()
products["Product1"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")
products["Product2"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")
products["Product3"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")
products["Product4"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")
products["Product5"] = ("Name", "Date of Production", "Name of Customer", "Product Expiration Date", "Storage Code", "List of Raw Material Codes", "Description")

products.save_to_database(conn)

conn.close()


    Explanation for step 2

This code updates the RawMaterials and Products classes to include methods for saving and loading from a database (using SQLite). It also creates five raw materials and five products with dummy attribute values and saves them to a database.
To run this code, you will need to have the sqlite3 library installed. You can install it using pip:

pip install sqlite3

Once you have the library installed, you can run the code by saving it to a file (e.g. "storeroom.py") and running it with Python:

python storeroom.py

This will create the GUI with the "Capture", "Image", and "Report Generation" buttons, and you can use the "Capture" button to start the webcam and see the video in a window. The "Report Generation" button is currently not implemented and should be used to generate a report for the raw materials and products considering their expiration and statistical information.
Final answer
Yes, the code should run on its own as long as you have the necessary libraries installed. The code imports the cv2 library for accessing the webcam, the Tkinter library for creating the GUI, and the xlwt library for saving to Excel files.
To run the code, you will need to have these libraries installed. You can install them using pip:
pip install opencv-python
pip install tkinter
pip install xlwt

Once you have these libraries installed, you should be able to run the code by saving it to a file (e.g. "storeroom.py") and running it with Python:
python storeroom.py

This will create the GUI with the "Capture" and "Image" buttons, and you can use the "Capture" button to start the webcam and see the video in a window.
Was this answer helpful?