eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I was trying to detect all the corners in the image using harris corner detection in opencv(python). But due to the thickness of the line , I am getting multiple corners in a single corner . Is there something I can do to make this right. code import numpy as np import cv2 as cv filename = 'Triangle.jpg' img = cv.imread(filename) gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) gray = np.float32(gray) dst = cv.cornerHarris(gray,2,3,0.04) #result is dilated for marking the corners, not important dst = cv.dilate(dst,None) # Threshold for an optimal value, it may vary depending on the image. img[dst>0.01*dst.max()]=[0,0,255] cv.imshow('dst',img) if cv.waitKey(0) & 0xff == 27: cv.destroyAllWindows()   

Answer: If your expectation is to obtain a single corner point at every line intersection, then the following is a simple approach. Current scenario: # visualize the corners mask = np.zeros_like(gray) mask[dst>0.01*dst.max()] = 255   In the above, there are many (supposedly) corner points close to each other. Approach: The idea now is to preserve only one point that is in close proximity to each other, while discarding the rest. To do so, I calculate the distance of each corner to every other and keep those that exceed a threshold. # storing coordinate positions of all points in a list coordinates = np.argwhere(mask) coor_list = coordinates.tolist() # points beyond this threshold are preserved thresh = 20 # function to compute distance between 2 points def distance(pt1, pt2): (x1, y1), (x2, y2) = pt1, pt2 dist = math.sqrt( (x2 - x1)**2 + (y2 - y1)**2 ) return dist # coor_list_2 = coor_list.copy() # iterate for every 2 points i = 1 for pt1 in coor_list: for pt2 in coor_list[i::1]: if(distance(pt1, pt2) < thresh): # to avoid removing a point if already removed try: coor_list_2.remove(pt2) except: pass i+=1 # draw final corners img2 = img.copy() for pt in coor_list_2: img2 = cv2.circle(img2, tuple(reversed(pt)), 3, (0, 0, 255), -1)   Suggestions: To get more accurate result you can try finding the mean of all the points within a certain proximity. These would coincide close to the intersection of lines.
Question: Disclaimer: I'm a computer vision rookie. I have seen a lot of stack overflow posts of how to find a specific sub-image in a larger image. My usecase is a bit different since I don't want it to be specific and I'm not sure how I can do this (if it's even possible, but I have a feeling it should). I have a large datasets of images, of sometimes, some of this images are a combination of two or more other images of the dataset. I'd like to automatically crop theses "combinations" to isolate the sub-images. So the tasks would be to process each image of the dataset, and check if there are abnormal boundaries that could mean the image is a combination. Example using great stock images:  What I've tried: I've seen that houghs transform could be used for line detection in images but I'm couldn't achieve anything using this.

Answer:  Use cv.Sobel or other derivative kernel (with cv.filter2D) to find edges Sum along pixel columns to score each for "edginess". np.sum or np.mean do that. Some thresholding and np.argsort and indexing to find best candidates. cut (take slices out of array)  edge map:  edgy plot:  slices to take: array([[ 0, 578], [ 578, 1135], [1135, 1136]], dtype=int64)  Pictures:   complete notebook: https://gist.github.com/crackwitz/e1ba1ce7a6fba446288275d91f66261c
Question: First, I used np.array to perform operations on multiple matrices, and it was successful. import numpy as np import matplotlib.pyplot as plt f = np.array([[0.35, 0.65]]) e = np.array([[0.92, 0.08], [0.03, 0.97]]) r = np.array([[0.95, 0.05], [0.06, 0.94]]) d = np.array([[0.99, 0.01], [0.08, 0.92]]) c = np.array([[0, 1], [1, 0]]) D = np.sum(f@(e@r@d*c)) u = f@e I = np.sum(f@(e*np.log(e/u))) print(D) print(I)  Outcome: 0.14538525 0.45687371996485304  Next, I tried to plot the result using one of the elements in the matrix as a variable, but an error occurred. import numpy as np import matplotlib.pyplot as plt t = np.arange(0.01, 0.99, 0.01) f = np.array([[0.35, 0.65]]) e = np.array([[1-t, t], [0.03, 0.97]]) r = np.array([[0.95, 0.05], [0.06, 0.94]]) d = np.array([[0.99, 0.01], [0.08, 0.92]]) c = np.array([[0, 1], [1, 0]]) D = np.sum(f@(e@r@d*c)) u = f@e I = np.sum(f@(e*np.log(e/u))) plt.plot(t, D) plt.plot(t, I) plt.show()  It shows the error below: AttributeError Traceback (most recent call last) AttributeError: 'numpy.ndarray' object has no attribute 'log' The above exception was the direct cause of the following exception: TypeError Traceback (most recent call last) <ipython-input-14-0856df964382> in <module>() 10 11 u = f@e ---> 12 I = np.sum(f@(e*np.log(e/u))) 13 14 plt.plot(t, D) TypeError: loop of ufunc does not support argument 0 of type numpy.ndarray which has no callable log method  There was no problem with the following code, so I think there was something wrong with using np.array. import numpy as np import matplotlib.pyplot as plt t = np.arange(0.01, 0.99, 0.01) y = np.log(t) plt.plot(t, y) plt.show()  Any idea for this problem? Thank you very much.

Answer: You can't create a batch of matrices e from the variable t using the construct e = np.array([[1-t, t], [0.03, 0.97]])  as this would create a ragged array due to [1-t, t] and [0.03, 0.97] having different shapes. Instead, you can create e by repeating [0.03, 0.97] to match the shape of [1-t, t], then stack them together as follows. t = np.arange(.01, .99, .01) # shape (98,) _t = np.stack([t, 1-t], axis=1) # shape (98, 2) e = np.array([[.03, .97]]) # shape (1, 2) e = np.repeat(e, len(ts), axis=0) # shape (98, 2) e = np.stack([_t, e], axis=1) # shape (98, 2, 2)  After this, e will be a batch of 2x2 matrices array([[[0.01, 0.99], [0.03, 0.97]], [[0.02, 0.98], [0.03, 0.97]], [[0.03, 0.97], [0.03, 0.97]], [[0.04, 0.96], [0.03, 0.97]], ...  Finally, expand other variables in the batch dimension to take advantage of numpy broadcast to batch the calculation f = np.array([[0.35, 0.65]])[None,:] # shape (1,1,2) r = np.array([[0.95, 0.05], [0.06, 0.94]])[None,:] # shape (1,2,2) d = np.array([[0.99, 0.01], [0.08, 0.92]])[None,:] # shape (1,2,2) c = np.array([[0, 1], [1, 0]])[None,:] # shape (1,2,2)  and only sum across the last axis to get per-matrix result. D = np.sum(f@(e@r@d*c), axis=-1) # shape (98, 1) u = f@e I = np.sum(f@(e*np.log(e/u)), axis=-1) # shape (98, 1)
Question: I am trying to figure out the preferred way to manage session if exceptiion occurs while doing operation or committing the session. But a few examples in the document confuses me a little bit. The doc says to frame out a commit/rollback block, you can do something like this: # verbose version of what a context manager will do with Session(engine) as session: session.begin() try: session.add(some_object) session.add(some_other_object) except: session.rollback() raise else: session.commit()  While this example includes the commit() call in the try block: Session = sessionmaker(bind=engine) session = Session() session.begin() try: item1 = session.query(Item).get(1) item2 = session.query(Item).get(2) item1.foo = 'bar' item2.bar = 'foo' session.commit() except: session.rollback() raise  I wonder:  what's the difference between these two patterns? which is the preferred way for managing transaction? The doc says both example above can be succinctly illustrated with session.begin() context manager, so does it handle the exception occurs while commit() ?  Possible related answes: Here is a answer that exludes commit() from the try block, but the explanation in the comments doesn't seem sufficient to me. And this answer mentions about why including commit() in try block.

Answer: Both examples accomplish nearly the same thing. The transaction will be committed if there is no error, and rolled back if there is an error. The difference is that if there is an exception on the commit itself, the second example will roll back. It's uncommon to get an exception on the commit, but it's possible.
Question: I am learning Python and I'm using Pytest to check my code as I learn. Here is some sample code I have running: str = "I love pizza" str_list = list(str) print(str_list) print(len(str_list))  With expected result printed to stdout: ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'i', 'z', 'z', 'a'] 12  But if I run this test: def create_list_from_string(): str = "I love pizza" str_list = list(str) assert 123 == len(str_list)  I cannot get the assert to fail. I have other tests in the file that pass when expected and fail if I purposely edit them to make them fail. So I think I have Pytest set up correctly. I know Python uses indentation for code blocks, and I verified all the indentations are 4 spaces and there's no trailing tabs or spaces. I also know that assert is not broken and I'm making some kind of newbie mistake. Thanks!

Answer: Try making method name and test file starts with test_
Question: Apologies in advance for the obscure title. I wasn't sure how to phrase what I encountered. Imagine that you have a title of a book alongside its author, separated by -, in a variable title_author. You scraped this information from the web so it might very well be that this item is None. Obviously you would like to separate the title from the author, so you'd use split. But in case title_author is None to begin with, you just want both title and author to be None. I figured that the following was a good approach: title_author = "In Search of Lost Time - Marcel Proust" title, author = title_author.split("-", 1) if title_author else None, None print(title, author) # ['In Search of Lost Time ', ' Marcel Proust'] None  But to my surprise, title now was the result of the split and author was None. The solution is to explicitly indicate that the else clause is a tuple by means of parentheses. title, author = title_author.split("-", 1) if title_author else (None, None) print(title, author) # In Search of Lost Time Marcel Proust  So why is this happening? What is the order of execution here that lead to the result in the first case?

Answer: title, author = title_author.split("-", 1) if title_author else None, None  is the same as: title, author = (title_author.split("-", 1) if title_author else None), None  Therefore, author is always None  Explaination: From official doc  An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.  That is to say, the interrupter will look for (x,y)=(a,b) and assign value as x=a and y=b. In your case, there are two interpretation, the main differece is that :  title, author = (title_author.split("-", 1) if title_author else None), None is assigning two values (a list or a None and a None) to two variables and no unpacking is needed.  title, author = title_author.split("-", 1) if title_author else (None, None) is actually assigning one value (a list or a tuple) to two variable, which need an unpacking step to map two variables to the two values in the list/tuple.   As option 1 can be completed without unpacking, i.e. less operation, the interrupter will go with option 1 without explicit instructions.
Question: I am simply trying to do this from IPython.lib import passwd but I get this error In [1]: from IPython.lib import passwd --------------------------------------------------------------------------- ImportError Traceback (most recent call last) Input In [1], in <cell line: 1>() ----> 1 from IPython.lib import passwd ImportError: cannot import name 'passwd' from 'IPython.lib' (/home/ubuntu/anaconda3/lib/python3.9/site-packages/IPython/lib/__init__.py)  Googled the error but nothing.

Answer: I am facing the same issue with IPython version 8.4. It seems to me that the security lib is not present anymore. If you are using version 7x you should be able to import it with from IPython.lib.security import passwd  as denoted in https://ipython.readthedocs.io/en/7.x/api/generated/IPython.lib.security.html?highlight=passwd However, from version 8x the module security is missing...
Question: I can't find the documentation for branching in Airflow's TaskFlowAPI. I tried doing it the "Pythonic" way, but when ran, the DAG does not see task_2_execute_if_true, regardless of truth value returned by the previous task. @dag( schedule_interval=None, start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), catchup=False, tags=['test'], ) def my_dag(): @task() def task_1_returns_boolean(): # evaluate and return boolean value return boolean_value @task() def task_2_execute_if_true(): # do_something... outcome_1 = task_1_returns_boolean() if outcome_1: outcome_2 = task_2_execute_if_true() executed = my_dag()  What is the proper way of branching in TaskFlowAPI? Should I add one more function specifically for branching?

Answer: There's an example DAG in the source code: https://github.com/apache/airflow/blob/f1a9a9e3727443ffba496de9b9650322fdc98c5f/airflow/example_dags/example_branch_operator_decorator.py#L43. The syntax is: from airflow.decorators import task @task.branch(task_id="branching_task_id") def random_choice(): return "task_id_to_run"  It was introduced in Airflow 2.3.0.
Question: I have a Python virtual environment named venv in the user home directory. I would like to activate this virtual environment on login. I don't want to type source venv/bin/activate each time after login. I want to type python something.py and have it always use the virtual environment. How can I do this in the user's login scripts? The user is only ever going to do python development in this particular virtual environment.

Answer: There might be better ways of doing this but the simplest way I can think of is modifying .bashrc file if you are using an OS Like Ubuntu. In your .bashrc file you can add a line to start your virtual environment. An example could be adding the following to the bottom of your .bashrc file: source myvenv/bin/activate
Question: I would like to change the theme of my streamlit application that I am working on. I read that I should make a directory called .streamlit/ with a file called config.toml, after creating the .toml file, it does not update the appearance of my web application. Here is the link to the app itself: https://jensen-holm-sports-sim-app-app-4a49zi.streamlitapp.com/ Here is the code in my config.toml file that is not working [theme] base="dark" primaryColor="#213767" backgroundColor="#13710d" font="monospace"  and a link to all of the code that is on git hub: https://github.com/Jensen-holm/sports-sim-app any help is appriciated!! thank you

Answer: I just ran into the same problem. What I found out is that I had selected the "Dark" option via the hamburger menu at some point before creating the custom config file in the project directory. If you've done the same, the default order of precedence is overwritten and that choice is preserved. Otherwise, the default precedence should load the custom theme by default. Solution was to select the "Custom Theme" again from the hamburger menu. On every run after that changes in the custom config were reflecting properly. In my app, I'm keeping the menu hidden but it still works fine once you have set the "Custom Theme" option - you can go back and re-hide the menu (if that's your need).
Question: In my Python application, I have the following lines: for index, codec in enumerate(codecs): for audio in filter(lambda x: x['hls']['codec_name'] == codec, job['audio']): audio['hls']['group_id'].append(index)  How can I only trigger the append statement if the index hasn't been already appended previously?

Answer: Simply test if your index not in your list: for index, codec in enumerate(codecs): for audio in filter(lambda x: x['hls']['codec_name'] == codec, job['audio']): if index not in audio['hls']['group_id']: audio['hls']['group_id'].append(index)
Question: I'm using pre-commit for most of my Python projects, and in many of them, I need to use pylint as a local repo. When I want to commit, I always have to activate python venv and then commit; otherwise, I'll get the following error: black....................................................................Passed pylint...................................................................Failed - hook id: pylint - exit code: 1 Executable `pylint` not found  When I use vscode version control to commit, I get the same error; I searched about the problem and didn't find any solution to avoid the error in VSCode. This is my typical .pre-commit-config.yaml: repos: - repo: https://github.com/ambv/black rev: 21.9b0 hooks: - id: black language_version: python3.8 exclude: admin_web/urls\.py - repo: local hooks: - id: pylint name: pylint entry: pylint language: python types: [python] args: - --rcfile=.pylintrc 

Answer: you have ~essentially two options here -- neither are great (language: system is kinda the unsupported escape hatch so it's on you to make those things available on PATH) you could use a specific path to the virtualenv entry: venv/bin/pylint -- though that will reduce the portability. or you could start vscode with your virtualenv activated (usually code .) -- this doesn't always work if vscode is already running  disclaimer: I created pre-commit
Question: Python version 3.9, FastAPI version 0.78.0 I have a custom function that I use for application exception handling. When requests run into internal logic problems, i.e I want to send an HTTP response of 400 for some reason, I call a utility function. @staticmethod def raise_error(error: str, code: int) -> None: logger.error(error) raise HTTPException(status_code=code, detail=error)  Not a fan of this approach. So I look at from fastapi import FastAPI, HTTPException, status from fastapi.respones import JSONResponse class ExceptionCustom(HTTPException): pass def exception_404_handler(request: Request, exc: HTTPException): return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={"message": "404"}) app.add_exception_handler(ExceptionCustom, exception_404_handler)  The problem I run into with the above approach is the inability to pass in the message as an argument. Any thoughts on the whole topic?

Answer: Your custom exception can have any custom attributes that you want. Let's say you write it this way: class ExceptionCustom(HTTPException): pass  in your custom handler, you can do something like def exception_404_handler(request: Request, exc: HTTPException): return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={"message": exc.detail})  Then, all you need to do is to raise the exception this way: raise ExceptionCustom(status_code=404, detail='error message')  Note that you are creating a handler for this specific ExceptionCustom. If all you need is the message, you can write something more generic: class MyHTTPException(HTTPException): pass  def my_http_exception_handler(request: Request, exc: HTTPException): return JSONResponse(status_code=exc.status_code, content={"message": exc.detail})  app.add_exception_handler(MyHTTPException, my_http_exception_handler)  This way you can raise any exception, with any status code and any message and have the message in your JSON response. There's a detailed explanation on FastAPI docs
Question: I have a file which imports a function from another file, like shown below. file1.py: # import scipy.signal import file2 file2.foo()  file2.py: import scipy def foo(): scipy.signal.butter(2, 0.01, 'lowpass', analog=False)  When I run file1.py I get the following error:  File "file2.py", line 5, in foo scipy.signal.butter(2, 0.01, 'lowpass', analog=False) AttributeError: module 'scipy' has no attribute 'signal'  However, when I uncomment line 1 from file1.py (import scipy.signal) the error disappears. Why is this happening?

Answer: With scipy, you need to import the submodule directly with either import scipy.signal or from scipy import signal. Many submodule won't work if you just import scipy. You can read about the scipy api here
Question: I am looking for a way to change the background color (and the active color) of the tickbox of the ttk.Checkbutton  Optimally, the background color of the box should match the background's color? What is the command to modify this in the style.configure() method?

Answer: It is possible to change the colors of the tickbox using the options indicatorbackground and indicatorforeground. style.configure("TCheckbutton", indicatorbackground="black", indicatorforeground="white", background="black", foreground="white")  To change the active colors, you need to use style.map() instead of style.configure(). style.map("TCheckbutton", background=[("active", "darkgrey")])  Note that with some themes (e.g. OSX and Windows default themes), the above commands do not produce any effect because the tickbox is created with an image. If this is the case, you can change the theme to use, for instance, "alt" or "clam", with style.theme_use("clam"). If you want to keep using a theme that does not allow to change the tickbox color, you can use your own tickbox images instead: # create custom tickbox element img_unticked_box = tk.PhotoImage(file="/path/to/unticked_box_image") img_ticked_box = tk.PhotoImage(file="/path/to/ticked_box_image") style.element_create("tickbox", "image", img_unticked_box, ("selected", img_ticked_box)) # replace the checkbutton indicator with the custom tickbox in the Checkbutton's layout style.layout( "TCheckbutton", [('Checkbutton.padding', {'sticky': 'nswe', 'children': [('Checkbutton.tickbox', {'side': 'left', 'sticky': ''}), ('Checkbutton.focus', {'side': 'left', 'sticky': 'w', 'children': [('Checkbutton.label', {'sticky': 'nswe'})]})]})] )  I obtained the layout of the Checkbutton with style.layout("TCheckbutton") and then replaced 'Checkbutton.indicator' by 'Checkbutton.tickbox'.
Question: I have an environment.yml file, but don't want to use Conda: name: foo channels: - defaults dependencies: - matplotlib=2.2.2  Is it possible to have pip install the dependencies inside an environment.yml file as if it's a requirements.txt file? I tried pip install -r environment.yml and it doesn't work with pip==22.1.2.

Answer: No, pip does not support this format. The format it expects for a requirements file is documented here. You'll have to convert the environment.yml file to a requirements.txt format either manually or via a script that automates this process. However, keep in mind that not all packages on Conda will be available on PyPI.
Question: I'm currently switching from chrome 102 to 104 Beta (since 103 has an unfixed error for my use case with a python - selenium script) I installed the Chrome Beta 104 + the 104 chrome driver. When I start up the script it recognizes that it's a 104 driver but the driver itself searches for the chrome.exe application within the old chrome path: Where it searches right now: C:\Program Files\Google\Chrome\Application\chrome.exe Where it should search for: C:\Program Files\Google\Chrome Beta\Application\chrome.exe Is there some simple way of changing the binary path where the beta chrome driver searches for the exe? Something simple I could put within my python script would be lovely.

Answer: In that case, you should specify where selenium has to look to find your chrome executer with binary_location inside the Options class. Try this one out: Assuming your chromedriver.exe and python file are on the same folder, otherwise you'll have to specify the path of the chromedriver.exe as well. from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service options = Options() options.binary_location = r'C:\Program Files\Google\Chrome Beta\Application\chrome.exe' browser = webdriver.Chrome(options = options, service = Service("chromedriver.exe")) browser.get(your_url)