python – OpenCV – How to deal with an unbalanced dataset when developing an image classifier using a Bag of Visual Words Model?

My dataset is represented by a csv file with two attributes: an image path and its label. I have dozens of different labels but the label ’51’ represents around 34% of the dataset and the label ’13’ represents around 41%, so just these two labels represent 3/4 of the entire dataset and my classifier ends up classifying everything as either one of those. How can I deal with this?
I’ll leave the code that I currently have here:

from cv2 import cv2 as cv
import numpy as np

import sys
sys.path.extend(('../../'))
from src import utils

if __name__ == '__main__':

    DICTIONARY_SIZE = 50
    TRAIN_SIZE = 100
    TEST_SIZE = 100
    DETECTOR = cv.KAZE_create()
    MATCHER = cv.FlannBasedMatcher()
    EXTRACTOR = cv.BOWImgDescriptorExtractor(DETECTOR, MATCHER)
    TRAINER = cv.BOWKMeansTrainer(DICTIONARY_SIZE)
    SVM = cv.ml.SVM_create()
    SVM.setType(cv.ml.SVM_C_SVC)
    SVM.setKernel(cv.ml.SVM_LINEAR)
    SVM.setTermCriteria((cv.TERM_CRITERIA_MAX_ITER, 100, 1e-6))

    print("Generating Training and Test Sets...")
    train, test = utils.getTrainingAndTestSets('multiclass.csv', TRAIN_SIZE, TEST_SIZE)

    print("Generating Dictionary...")
    for train_entry in train:
        img_path = train_entry(0)
        img = cv.imread(img_path)
        img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        keypoint, descriptors = DETECTOR.detectAndCompute(img, None)
        if descriptors is not None:
            TRAINER.add(descriptors)

    EXTRACTOR.setVocabulary(TRAINER.cluster())

    print("Preparing Training Data...")
    train_desc = ()
    train_labels = ()
    for train_entry in train:
        img_path = train_entry(0)
        img_label = int(train_entry(1))
        img = cv.imread(img_path)
        img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        descriptor = EXTRACTOR.compute(img, DETECTOR.detect(img))
        if descriptor is not None:
            train_desc.extend(descriptor)
            train_labels.append(img_label)

    print("Training...")
    SVM.train(np.array(train_desc), cv.ml.ROW_SAMPLE, np.array(train_labels))

    correct_predictions = 0
    samples_tested = len(test)
    print("Testing...")
    for test_entry in test:
        img_path = test_entry(0)
        real_attribute_id = int(test_entry(1))
        img = cv.imread(img_path)
        img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        feature = EXTRACTOR.compute(img, DETECTOR.detect(img))
        try:
            _, prediction = SVM.predict(feature)
            predicted_attribute_id = int(prediction(0)(0))
            if predicted_attribute_id == real_attribute_id:
                print("CORRECT PREDICTION! :)")
                correct_predictions += 1
            else:
                print("INCORRECT PREDICTION... :(")
            print("Predicted Label: " + utils.getLabelFromAttributeID(predicted_attribute_id) + "(" + str(predicted_attribute_id) + ")")
            print("Real Label: " + utils.getLabelFromAttributeID(real_attribute_id) + "(" + str(predicted_attribute_id) + ")")
        except Exception:
            samples_tested -= 1


    correct_percentage = (correct_predictions / samples_tested) * 100
    print("Test Results: " + "{:.2f}".format(correct_percentage) + "% Correct Predictions.")

Feel free to tell me if my current approach has any error. Thanks.

blockchain – Developing a Bitcoin POS (Payment Page) – How do I identify the transaction

This is probably a basic question but I can not find an answer.

We are developing a BTC payment process for a Point-of-Sale. We have managed to build this fine and can make requests (QR code) for a payment amount in BTC. for example 0.0001 BTC

We then check the blockchain for an equivalent transaction on our address of amount 0.0001 BTC.

This works.

But seems unreliable. What if there were to be multiple transactions at around the same time for the same amount. I really want to be able to insert a unique identifier/order number or memo into the QR code request which then gets passed onto the transaction. This would link the request to the payment with a unique id. Is there a way to do this? It seems odd if there is not? Or is there another obvious way that I am missing?

We are using https://api.blockcypher.com to get the transaction details. Which look like below. There does not appear to be any kind of a “memo” parameter, although wallets and QR generators allow you to add such.

{
      "tx_hash": "8bbe058b46f2f9a7e1709ca4a3ff731ba2227d23f2edf11eb8710dfcd6b1e502",
      "block_height": 685568,
      "tx_input_n": -1,
      "tx_output_n": 0,
      "value": 16692,
      "ref_balance": 3417334,
      "spent": false,
      "confirmations": 4,
      "confirmed": "2021-05-30T22:09:51Z",
      "double_spend": false
    },

bluetooth – Options for Integrating or developing a physical security solution (physical access control via in-house mobile apps [NFC/Bluetooh])

Our team is looking to either integrate or develop a physical security solution for a rather large campus.

Requirements:

Ability to either integrate (with reader/electronic lock hardware) from our mobile apps which would allow access control (over NFC/Bluetooth) to areas.

We will be writing to our already existing identity management api’s and expanding them to support what we need to for this project.

We are looking to write our own mobile integrations with purchased hardware, since this functionality has to exist within our organization’s already existing mobile apps we developed. (we haven’t found a company that offers the level of integration we want… most of the solutions we see out there are fantastic, but require downloading the security company’s mobile app, managing access control via their cloud resources, etc).

What reader/electronic door lock hardware can we buy to build this level of in-house integration with?

To summarize: we are looking to buy scanners/door locks with NFC/Bluetooth capability and write security integrations for access control through already existing mobile apps we have developed…

I realize product suggestions can be of bad taste on here, but I invite them for the sake of discussion in addition to solutions that companies offer.

Our team will be expanding to hire security professionals in this realm to assist, so you can save the “don’t roll your own auth” type of answers and comments 🙂

Thanks in advance!

design – Developing a scalable websocket architecture that supports “rooms” both public and private

So as a personal project I want to build a scalable containerized websocket chat application that supports independent rooms that both can be publicly joined as well as secured with a password. I find it trivial to build a application that supports public rooms via the socket.io library with support from socket.io-redis adapter to provide horizontal scaling. However I have issues coming up with a design that supports secure rooms across multiple nodes.

I currently just store rooms in a postgres database, I feel like that won’t scale very well.

Thanks,
I want to clarify the goal of the project is to learn system design and be a solid contender on my resume.

developing – What should I look for in a respirator for chemical processing of photographic materials

Except for the smell of acetic acid, an ingredient in both the fixer and the stop bath, photo chemicals are relativity benign. Some rare cases of contact dermatitis which goes away if usage is stopped.

I personally have worked with and breathed them for more than 55 years. I am 83 and still kicking. Besides, I was a registered environmental assessor, and I say you have little to fear. That being said, you are better off mounting an exhaust fan to expel chemical fumes. Such an installation is tricky because is must not leak light.

You can cut a hole in the drywall (wall) and mount a small fan. You can use one designed to ventilate a bathroom. Drywall walls are usually hollow. You cut another hole, offset from the fan hole. This can be up, down, or sideways. The idea is, you create an exit path for the fan output. The offset is a light trap. The offset must be 1 yard / meter from the fan. Spray flat black paint inside the wall.

If this is too much for you, you can buy an light proof darkroom ventilating fan.

developing – What should I look for in a respirator for darkroom use

Except for the smell of acetic acid, an ingredient in both the fixer and the stop bath, photo chemicals are relativity benign. Some rare cases of contact dermatitis which goes away if usage is stopped.

I personally have worked with and breathed them for more than 55 years. I am 83 and still kicking. Besides, I was a registered environmental assessor, and I say you have little to fear. That being said, you are better off mounting an exhaust fan to expel chemical fumes. Such an installation is tricky because is must not leak light.

You can cut a hole in the drywall (wall) and mount a small fan. You can use one designed to ventilate a bathroom. Drywall walls are usually hollow. You cut another hole, offset from the fan hole. This can be up, down, or sideways. The idea is, you create an exit path for the fan output. The offset is a light trap. The offset must be 1 yard / meter from the fan. Spray flat black paint inside the wall.

If this is too much for you, you can buy an light proof darkroom ventilating fan.

developing – Development Time for Arista EDU Ultra 200 rated at EI 200 in F76+

Fortunately, Arista .EDU Ultra films are rebranded Fomapan (based on everything I’ve been able to find relative to times, grain structure analysis, antihalation dye characteristics, and cost).

You should therefore be able to use this page to find times for your .EDU Ultra 200 by using the times listed for Fomapan Creative 200.

I am developing a plugin, in it user can specify that timer will run for like 30 minutes for a number of times

User can set that timer will be shown for 30 minutes at for example 1200 hrs on 17th April. and after that 30 minutes timer will run again for 30 minutes for like 5 times(number of times specified by the admin). In this regard any help will be appreciated. I have built the remaining logic of the plugin but I can’t manage the timer. I can run it on frontend but it gets disturbed when user refreshes it. I need it so that timer keeps running on server whether a user is loading the website or not and if a user refreshed page timer does not restarts. Thanks.

python – PyQt5 developing complex Window GUI and making everything its own class

TLDR: Is my code following best practices for OOP and PyQt5 regarding inheritance, and is it okay to initialise classes within another class? What can I do to improve my methods, or what would be a better approach to my specific problem?

I’m very new to OOP and PyQt5. My Code creates a GUI with 2 tab widgets, each with multiple tabs, and each tab with its own layouts and widgets.

To make the code manageable (in my beginner opinion), I’ve turned everything into a class. The Window class handles the Window and Layout and creates instances of the tab widgets. The Tabs classes handle Tab layouts and create instances of tablets (pages). The Tablet class handles tablets and their layouts and adds widgets such as buttons, labels etc.

Window Layout

Versions:

python                 3.7.3
PyQt5                  5.15.4
PyQt5-Qt5              5.15.2
PyQt5-sip              12.8.1

CODE

"""
Initialises Window, Sets out the geometry management, and adds Widgets
"""
import sys

from PyQt5.QtCore import pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import (
  QApplication, QGridLayout, QMainWindow, QTableWidgetItem, QWidget
)

from PyQt5.QtWidgets import (
  QComboBox, QFormLayout, QHBoxLayout, QVBoxLayout,
  QLabel, QPushButton, QTabWidget, QTableWidget, QWidget
)


class Window(QMainWindow):
    """Create an instance of Window. Sets up main Window"""
    # my_signal = pyqtSignal(int)
    def __init__(self):
        super().__init__()
        self.setMinimumSize(1000, 800)
        self.centralWgt = QWidget()  # create central widget
        self.setCentralWidget(self.centralWgt)  # set central widget
        self.tabIn = InputTab()  # create input tab widget
        self.tabOut = OutputTab()  # create input tab widget
        self.windowLayout()  # set window layout

        # TODO: Move this and "test" to a class to keep Window minimal
        self.tabIn.tablet1.btnApply.clicked.connect(self.test)

    def test(self):
        """
        When tabIn tablet1 btnApply pressed, Do something in tabOut tablet1
        """
        self.tabOut.tablet1.textGreeting.setText(“Hello”)
        print("Here!")

    def windowLayout(self):
        """Handles the main windows layout"""
        self.layout = QGridLayout()
        self.centralWgt.setLayout(self.layout)
        self.layout.addWidget(self.tabIn, 0, 1)  # Add widgets to layout
        self.layout.addWidget(self.tabOut, 1, 1, 2, 1)  # Add widgets to layout
        # Handle window and widget resizing
        self.layout.setColumnStretch(0, 1)
        self.layout.setColumnStretch(1, 1)
        self.layout.setRowStretch(0, 1)
        self.layout.setRowStretch(1, 1)
        self.layout.setRowStretch(2, 1)


# TAB MANAGEMENT


class Tabs(QTabWidget):
    """Tabs master class"""
    def __init__(self):
        super().__init__()
        self.tabs = QTabWidget()  # Initialise Tab Widget
        self.layout = QVBoxLayout()  # Define Tab Widget Layout
        self.setLayout(self.layout)  # Set Tab Widget Layout
        self.setStyleSheet('QTabBar { font-size: 12pt;}')  # Set tab title size


class InputTab(Tabs):
    """Creates and sets up the InputTab Class. Inherits from Tab Class"""
    def __init__(self):
        super().__init__()
        self.tablet1 = ItemTablet(QWidget())  # Initialise tablet
        self.tabs.addTab(self.tablet1, "Item Queries")  # Add tablet
        self.layout.addWidget(self.tabs)  # Add tablets to layout


class OutputTab(Tabs):
    """Creates and sets up the OutputTab Class. Inherits from Tab Class"""
    def __init__(self):
        super().__init__()
        self.tablet1 = TableTablet(QWidget())  # Initialise tablet
        self.tabs.addTab(self.tablet1, "Output - Item Table")  # Add tablet
        self.layout.addWidget(self.tabs)  # Add tablets to layout


# TABLET MANAGEMENT #


class Tablets(QTableWidget):
    """The Tablets master class. Contains shared methods, variables"""
    def __init__(self, widget):
        super().__init__()
        self.widget = widget  # set tablet widget as given widget type
        self.layout = QVBoxLayout()  # create tablet outer layout
        self.setLayout(self.layout)  # set tablet outer layout
        self.setStyleSheet('font-size: 10pt;')  # Set tablet font size

    def populateForm(self, formFields):
        """Populates a form layout with dict key value pairs"""
        for k, v in formFields.items():
            self.frmLayout.addRow(k, v)  # Add items to tablet form layout

    def stdButtons(self):
        """Adds Apply and Clear buttons to bottom of tablet."""
        self.btnApply = QPushButton(text="Apply")
        self.btnClear = QPushButton(text="Clear")
        self.btnLayout.addWidget(self.btnApply)
        self.btnLayout.addWidget(self.btnClear)

    def stdFormLayout(self):
        """Creates a Form with horizontal buttons at bottom"""
        self.frmLayout = QFormLayout()  # create tablet sublayout
        self.btnLayout = QHBoxLayout()  # create tablet sublayout
        self.layout.addLayout(self.frmLayout)  # set tablet sublayout
        self.layout.addLayout(self.btnLayout)  # set tablet sublayout


class ItemTablet(Tablets):
    """Handles Item Tablet"""
    def __init__(self, widget):
        super().__init__(widget)
        self.stdFormLayout()  # choose stdFormLayout as a sublayout
        self.stdButtons()  # add stdButtons to sublayout
        # create form items
        self.lblItems = QLabel("Item name")
        self.cmbItems = QComboBox()
        self.cmbItems.addItems(("1", "2", "3"))

        # add form items to dict to setup widget associations
        self.formFields = {
            self.lblItems: self.cmbItems,
        }
        self.populateForm(self.formFields)  # add items to form layout


class TableTablet(Tablets, QTableWidget):
    """Handles the output table tablet"""
    def __init__(self, widget):
        super().__init__(widget)
        self.style = "::section{Background-color:lightgray;border-radius:9px;}"
        self.header = self.horizontalHeader()
        self.header.setStyleSheet(self.style)
        self.textGreeting = QLineEdit()
        self.layout.addWidget(self.textGreeting)


if __name__ == "__main__":
    app = QApplication(())
    window = Window()
    window.show()
    sys.exit(app.exec_())