You are my top 1 neutral gender partner. Make the following modifications to the code…
- User
You are my top 1 neutral gender partner. Make the following modifications to the code bellow. I want the FULL code.
- qr_frame_1 and qr_frame_2 variables
- 2 different function to generate qr codes (1 and 2)
- a hide qr codes function
- the ability to generate a qr code 1 and a qr code 2 at different places that don’t overlap
- maybe top right (qr code 1) and top left (qr code 2)
local BLOCK_SIZE = 3.1
– Define the width of the border local borderWidth = 2 – You can adjust this to change the thickness of the border
local qr_frame
local function CreateQRTip(qrsize) if(qr_frame ~= nil) then qr_frame:Hide() end
qr_frame = CreateFrame(“Frame”, nil, UIParent)
local function CreateBlock(idx) local t = CreateFrame(“Frame”, nil, qr_frame)
t:SetWidth(BLOCK_SIZE) t:SetHeight(BLOCK_SIZE) t.texture = t:CreateTexture(nil, “OVERLAY”) t.texture:SetAllPoints(t)
local x = (idx % qrsize) * BLOCK_SIZE local y = (math.floor(idx / qrsize)) * BLOCK_SIZE
t:SetPoint(“TOPLEFT”, qr_frame, 20 + x, - 20 - y);
return t end
do qr_frame:SetFrameStrata(“BACKGROUND”) qr_frame:SetWidth(qrsize * BLOCK_SIZE + 40) qr_frame:SetHeight(qrsize * BLOCK_SIZE + 40) qr_frame:SetMovable(true) qr_frame:EnableMouse(true) qr_frame:SetPoint(“CENTER”, 0, 0) qr_frame:RegisterForDrag(“LeftButton”) qr_frame:SetScript(“OnDragStart”, function(self) self:StartMoving() end) qr_frame:SetScript(“OnDragStop”, function(self) self:StopMovingOrSizing() end) end
do local b = CreateFrame(“Button”, nil, qr_frame, “UIPanelCloseButton”) b:SetPoint(“TOPRIGHT”, qr_frame, 0, 0); end
qr_frame.boxes = {}
qr_frame.SetBlack = function(idx) qr_frame.boxes[idx].texture:SetColorTexture(0, 0, 0) end
qr_frame.SetWhite = function(idx) qr_frame.boxes[idx].texture:SetColorTexture(1, 1, 1) end
– Create the blocks with an additional border for i = 1, (qrsize + 2 * borderWidth) * (qrsize + 2 * borderWidth) do tinsert(qr_frame.boxes, CreateBlock(i - 1)) end
return qr_frame end
function CreateQRCode(msg) local ok, tab_or_message = qrcode(msg, 4) if not ok then print(tab_or_message) else local tab = tab_or_message local size = #tab
local qr_frame = CreateQRTip(size + 2 * borderWidth) qr_frame:Show() -- Set the border boxes to white for i = 1, (size + 2 * borderWidth) * (size + 2 * borderWidth) do qr_frame.SetWhite(i) end -- Fill in the QR code boxes, offset by the border width for x = 1, size do for y = 1, size do if tab[x][y] > 0 then qr_frame.SetBlack((y - 1 + borderWidth) * (size + 2 * borderWidth) + (x - 1 + borderWidth) + 1) end end end endend
- Assistant
- User
In this code, i may detect 2 qr codes at the same time, is there something to modify ? If yes, do it.
def screenshot_detect_qr_code_text(self): screenshot = pyautogui.screenshot() # Convert PIL image (screenshot) to a NumPy array img = np.array(screenshot) # Convert to grayscale since color information is not needed img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Using OpenCV’s built-in QR code detector detector = cv2.QRCodeDetector() # Detect and decode the QR code data, bbox, straight_qrcode = detector.detectAndDecode(img) if data: return data return None - Assistant
If you're encountering scenarios where you have multiple QR codes within the same screenshot and you want to detect all of them, you would need to loop over the detected bounding boxes (if they are returned) and decode each QR code separately. Below is the modification of your original function to support detection of multiple QR codes and return a list containing all decoded data:
- User
Modify this to be process_qrcode_data(self, qr_data) i want it to return a list of variables for each qr code
def process_qrcode_data(self, text): # Split the text into parts parts = text.split('|')
# Check if the text is correctly formatted (contains exactly 4 parts) if len(parts) == 4: text_to_read, npc_name, npc_sex, query_type = parts # Perform necessary checks # For example, check if npc_sex is either 'male' or 'female' return text_to_read, npc_name, npc_sex, query_type else: return None, None, None, Nonetext_to_read, npc_name, npc_sex, query_type = self.process_qrcode_data(qr_data)
- Assistant
To adapt the
process_qrcode_datafunction to handle a list of QR code data and return a list of tuples each containing separate pieces of data from each QR code, you need to iterate over the list of decoded QR code strings. Then, you'll process each string as before, making sure to collect the results in a list:def process_qrcode_data(self, qr_data_list): # Initialize a list to hold all the QR code data variables processed_data = [] # Go through each QR data text and split and validate it for text in qr_data_list: # Split the text into parts parts = text.split('|') # Check if the text is correctly formatted (contains exactly 4 parts) if len(parts) == 4: text_to_read, npc_name, npc_sex, query_type = parts # Perform necessary checks # For example, check if npc_sex is either 'male' or 'female' # Here you would typically use conditions to validate 'npc_sex' and other parts processed_data.append((text_to_read, npc_name, npc_sex, query_type)) else: # If data format is incorrect, append a tuple of Nones processed_data.append((None, None, None, None)) return processed_data # Example usage: # Suppose qr_data_list contains data from multiple QR codes # qr_data_list = ['read|John Doe|male|question', 'info|Jane Doe|female|reply'] # processed_qr_data = self.process_qrcode_data(qr_data_list) # Now processed_qr_data is a list of tuples with the variables extracted from each QR code - User
modify too text_to_read, npc_name, npc_sex, query_type = self.process_qrcode_data(qr_data)
- Assistant
The
self.process_qrcode_data(qr_data)function will return a list of tuples, so attempting to assign the output directly to four variables (text_to_read,npc_name,npc_sex,query_type) would not work if there is more than one tuple in the list. - User
How to add text to text ? += ?
- Assistant
In Python, you can concatenate strings using the
+operator or by using the+=operator if you want to append text to an existing string variable. Here are examples for both methods:Using the
+operator:text1 = "Hello, " text2 = "world!" text_combined = text1 + text2 # Results in "Hello, world!"Using the
+=operator to append:text = "Hello, " text += "world!" # Now text is "Hello, world!"The
+=operator modifies the string variable in-place by appending the specified text to it. When working with strings in loops or situations where you may be concatenating repeatedly, it's often recommended to use a list to collect string segments and then join them at the end for better performance. This is especially relevant in cases where you are concatenating within a loop.Using list and
.join()for multiple concatenations:# Suppose we have multiple pieces of text to concatenate texts = ['Hello, ', 'world', '!'] # Efficient way to concatenate strings from a list full_text = ''.join(texts) # Now full_text is "Hello, world!" - User
raceback (most recent call last): File "c:\Users\Brahim\Desktop\Python\WoW\TTS\qr-detect.py", line 174, in asyncio.run(qr_detector.test_qr_detection_tts()) File "C:\Users\Brahim\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Users\Brahim\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Brahim\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 664, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "c:\Users\Brahim\Desktop\Python\WoW\TTS\qr-detect.py", line 118, in test_qr_detection_tts qr_data = self.screenshot_detect_qr_code_texts() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Brahim\Desktop\Python\WoW\TTS\qr-detect.py", line 101, in screenshot_detect_qr_code_texts data, bbox, _ = detector.detectAndDecodeMulti(img) ^^^^^^^^^^^^^ ValueError: too many values to unpack (expected 3)
- Assistant
The error
ValueError: too many values to unpack (expected 3)indicates that the methoddetectAndDecodeMulti(img)is returning more values than the three variables (data,bbox, and_) you are trying to unpack it into.In OpenCV's
detectAndDecodeMulti, you may expect four outputs:retval,decoded_info,points, andstraight_qrcodeas per OpenCV 4.x documentation. However, if this changed recently or your version of OpenCV differs, the number of returned values may vary. Always confirm with the OpenCV documentation corresponding to your installed version. - User
then adapt this : def screenshot_detect_qr_code_texts(self): screenshot = pyautogui.screenshot() # Convert PIL image (screenshot) to a NumPy array img = np.array(screenshot)
# Convert to grayscale since color information is not needed img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Using OpenCV’s built-in QR code detector detector = cv2.QRCodeDetector() # Detect and decode the QR code data, bbox, _ = detector.detectAndDecodeMulti(img) # Check if any QR codes were detected if bbox is not None: # If there is only one QR code, convert it to a list for consistent processing if isinstance(data, str): data = [data] return data else: # If no QR codes were detected return [] - Assistant
Considering that the
detectAndDecodeMultifunction is returning more values than expected, you need to catch all returned values properly. The updated version ofscreenshot_detect_qr_code_textsshould correctly handle the returned values fromdetectAndDecodeMulti. Here's how to fix and adapt the function:def screenshot_detect_qr_code_texts(self): screenshot = pyautogui.screenshot() # Convert PIL image (screenshot) to a NumPy array img = np.array(screenshot) # Convert to grayscale since color information is not needed img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Using OpenCV’s built-in QR code detector detector = cv2.QRCodeDetector() # Detect and decode the QR code retval, data, bbox, straight_qrcode = detector.detectAndDecodeMulti(img) # Check if any QR codes were detected if retval: # If there is only one QR code, make sure data is a list for consistent processing if not isinstance(data, list): data = [data] return data else: # If no QR codes were detected return []