eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to detect playing cards and transform them to get a bird's eye view of the card using python opencv. My code works fine for simple cases but I didn't stop at the simple cases and want to try out more complex ones. I'm having problems finding correct contours for cards.Here's an attached image where I am trying to detect cards and draw contours:  My Code: path1 = "F:\\ComputerVisionPrograms\\images\\cards4.jpeg" g = cv2.imread(path1,0) img = cv2.imread(path1) edge = cv2.Canny(g,50,200) p,c,h = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) rect = [] for i in c: p = cv2.arcLength(i, True) ap = cv2.approxPolyDP(i, 0.02 * p, True) if len(ap)==4: rect.append(i) cv2.drawContours(img,rect, -1, (0, 255, 0), 3) plt.imshow(img) plt.show()  Result:  This is not what I wanted, I wanted only the rectangular cards to be selected but since they are occluding one another, I am not getting what I expected. I believe I need to apply morphological tricks or other operations to maybe separate them or make the edges more prominent or may be something else. It would be really appreciated if you could share your approach to tackle this problem. A few more examples requested by other fellows:  

Answer: There are lots of approaches to find overlapping objects in the image. The information you have for sure is that your cards are all rectangles, mostly white and have the same size. Your variables are brightness, angle, may be some perspective distortion. If you want a robust solution, you need to address all that issues. I suggest using Hough transform to find card edges. First, run a regular edge detection. Than you need to clean up the results, as many short edges will belong to "face" cards. I suggest using a combination of dilate(11)->erode(15)->dilate(5). This combination will fill all the gaps in the "face" card, then it "shrinks" down the blobs, on the way removing the original edges and finally grow back and overlap a little the original face picture. Then you remove it from the original image. Now you have an image that have almost all the relevant edges. Find them using Hough transform. It will give you a set of lines. After filtering them a little you can fit those edges to rectangular shape of the cards. dst = cv2.Canny(img, 250, 50, None, 3) cn = cv2.dilate(dst, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))) cn = cv2.erode(cn, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))) cn = cv2.dilate(cn, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))) dst -= cn dst[dst < 127] = 0 cv2.imshow("erode-dilated", dst) # Copy edges to the images that will display the results in BGR cdstP = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR) linesP = cv2.HoughLinesP(dst, 0.7, np.pi / 720, 30, None, 20, 15) if linesP is not None: for i in range(0, len(linesP)): l = linesP[i][0] cv2.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 255, 0), 2, cv2.LINE_AA) cv2.imshow("Detected edges", cdstP)  This will give you following:
Question: When passing around functions, I normally type hint them with typing.Callable. The docs for collections.abc.Callable state that it has four dunder methods:  class collections.abc.Callable ABCs for classes that provide respectively the methods __contains__(), __hash__(), __len__(), and __call__().  At one point, I want to check if there is a __wrapped__ attribute on a function. This works fine at runtime via a check with hasattr(func, "__wrapped__"). When static type checking with mypy, it reports: error: "Callable[..., Any]" has no attribute "__wrapped__" [attr-defined]. This makes sense to me, as Callable isn't supposed to have a __wrapped__ attribute. How can I properly type hint a Callable with a __wrapped__ attribute? Is there some other type hint or workaround I can do?  Code Sample I am using mypy==0.782 and Python==3.8.2: from functools import wraps from typing import Callable def print_int_arg(arg: int) -> None: """Print the integer argument.""" print(arg) @wraps(print_int_arg) def wrap_print_int_arg(arg: int) -> None: print_int_arg(arg) # do other stuff def print_is_wrapped(func: Callable) -> None: """Print if a function is wrapped.""" if hasattr(func, "__wrapped__"): # error: "Callable[..., Any]" has no attribute "__wrapped__" [attr-defined] print(f"func named {func.__name__} wraps {func.__wrapped__.__name__}.") print_is_wrapped(wrap_print_int_arg) 

Answer: Obviously the easy answer is to add a # type: ignore comment. However, this isn't actually solving the problem, IMO. I decided to make a type stub for a callable with a __wrapped__ attribute. Based on this answer, here is my current solution: from typing import Callable, cast class WrapsCallable: """Stub for a Callable with a __wrapped__ attribute.""" __wrapped__: Callable __name__: str def __call__(self, *args, **kwargs): ... def print_is_wrapped(func: Callable) -> None: """Print if a function is wrapped.""" if hasattr(func, "__wrapped__"): func = cast(WrapsCallable, func) print(f"func named {func.__name__} wraps {func.__wrapped__.__name__}.")  And mypy now reports Success: no issues found in 1 source file. I feel as if this is a lot of boiler-plate code, and would love a more streamlined answer.
Question: I've written some code to communicate between two Raspberry Pi's, using identical HC-12 433Mhz transceivers. I was able to successfully echo between the two Pi's using a direct serial connection and echo/cat, however am unable to replicate this using HC-12s, which theoretically work by a similar principal. I'm using the port ttyAMA0 on both for this example, but ttyS0 is also available and have tried every combination of these ports. The following code is common to both the sending and receiving, just writing once for sake of brevity: import serial import time ser = serial.Serial( port = "/dev/ttyAMA0", baudrate = 9600, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS ) print("Serial status: " + str(ser.isOpen()))  This is the sending program: while True: print("Sending...") ser.write("hello\n".encode()) time.sleep(1)  And the receiving program: while True: print("Receiving...") data = ser.readlines() print(data.decode())  The sending program simply loops as expected, but the receiver prints "Receiving...", and then nothing. When I keyboard interrupt the receiving program at that point, it says it is currently up to data = ser.readlines(). Any help would be much appreciated - I've spent the better part of the last week trawling and exhausting forums and READMEs to no avail, and this is literally my last option. Am close to insanity on this one!

Answer: The pyserial readlines() function relies on the timeout parameter to know when end-of-file is reached - this is warned about in the doco. So with no timeout, the end never occurs, so it keeps buffering all lines read forever. So you can just add a timeout to the serial port open, and your existing code will begin to work. ser = serial.Serial( port = "/dev/ttyAMA0", baudrate = 9600, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS, timeout = 2 # seconds # <-- HERE )  A better approach might be to use readline() (note singular, no 's'), for each line in turn: print( "Receiving..." ) while True: try: data = ser.readline() print( data.decode() ) # TODO - something with data except: print( "Error reading from port" ) break  As that will allow the code to act on the input line-by-line.
Question: Following is the logging snippet I have used in my django settings.py file. All the GET,POST requests are getting written to log but when i wrote logger.info("print something"), its not getting printed/captured in console as well as the log file Please suggest a workaround to capture logger.info() logs views.py import logging logger = logging.getLogger(__name__) def custom_data_generator(request): logger.info("print somethig") # NOT GETTING CAPTURED IN LOG FILE return somethig   settings.py (DEBUG = True and DEBUG_MODE = False in settings.py file) LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'simple': { 'format': '[%(asctime)s] %(levelname)s|%(name)s|%(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S', }, }, 'handlers': { 'applogfile': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', 'filename': '/home/mahesh/Documents/refactor/unityapp/unity/media/myproject.log', 'backupCount': 10, 'formatter': 'simple', }, 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'simple' } }, 'loggers': { 'django': { 'handlers': ['applogfile', 'console'], 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'), } } }  log data generated as follows [2020-07-07 11:43:25] ERROR|django.server|"GET /a11y/project-dashboard/? refnum=ACGLOBAL&env_id=4 HTTP/1.1" 500 92016 [2020-07-07 12:05:21] INFO|django.server|"GET /admin/ HTTP/1.1" 200 59501 [2020-07-07 12:05:21] INFO|django.server|"GET /admin/ HTTP/1.1" 200 59501 [2020-07-07 12:05:21] INFO|django.server|"GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692 [2020-07-07 12:05:21] INFO|django.server|"GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184 [2020-07-07 12:05:21] INFO|django.server|"GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876 [2020-07-07 12:05:26] INFO|django.server|"GET /admin/accessibility/axe_json/ HTTP/1.1" 200 1886434 [2020-07-07 12:05:27] INFO|django.server|"GET /admin/jsi18n/ HTTP/1.1" 200 3223 [2020-07-07 12:05:27] INFO|django.server|"GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 200 280364 [2020-07-07 12:05:27] INFO|django.server|"GET /static/admin/js/vendor/xregexp/xregexp.js HTTP/1.1" 200 128820 [2020-07-07 12:05:34] INFO|django.server|"GET /admin/accessibility/axe_json/?page_id=https%3A%2F%2Fjobs.chegg.com%2Fapplythankyou HTTP/1.1" 200 1868950 [2020-07-07 12:05:35] INFO|django.server|"GET /admin/jsi18n/ HTTP/1.1" 200 3223 

Answer: It's probably because your views module doesn't have a logging level set, so it will inherit the root logger's default level of WARNING. If you add a root entry with a level of INFO, similarly to the documented examples, you should see messages from other modules. Alternatively you can specify logger names under the loggers key for your specific module hierarchy, whatever that is. (Your example only overrides the WARNING level for modules in the django hierarchy, i.e. code in Django itself.)
Question: I have a list of dictionary and a string. I want to add a selected attribute in each dictionary inside the list. I am wondering if this is possible using a one liner. Here are my inputs: saved_fields = "apple|cherry|banana".split('|') fields = [ { 'name' : 'cherry' }, { 'name' : 'apple' }, { 'name' : 'orange' } ]  This is my expected output: [ { 'name' : 'cherry', 'selected' : True }, { 'name' : 'apple', 'selected' : True }, { 'name' : 'orange', 'selected' : False } ]  I tried this: new_fields = [item [item['selected'] if item['name'] in saved_fields] for item in fields] 

Answer: I don't necessarily think "one line way" is the best way. s = set(saved_fields) # set lookup is more efficient for d in fields: d['status'] = d['name'] in s fields # [{'name': 'cherry', 'status': True}, # {'name': 'apple', 'status': True}, # {'name': 'orange', 'status': False}]  Simple. Explicit. Obvious. This updates your dictionary in-place, which is better if you have a lot of records or other keys besides "name" and "status" that you haven't told us about.  If you insist on a one-liner, this is one preserves other keys: [{**d, 'status': d['name'] in s} for d in fields] # [{'name': 'cherry', 'status': True}, # {'name': 'apple', 'status': True}, # {'name': 'orange', 'status': False}]  This is list comprehension syntax and creates a new list of dictionaries, leaving the original untouched. The {**d, ...} portion is necessary to preserve keys that are not otherwise modified. I didn't see any other answers doing this, so thought it was worth calling out. The extended unpacking syntax works for python3.5+ only, for older versions, change {**d, 'status': d['name'] in s} to dict(d, **{'status': d['name'] in s}).
Question: I want to add a new column, Category, in each of my 8 similar dataframes. The values in this column are the same, they are also the df name, like df1_p8 in this example. I have used: In: df61_p8.insert(3,"Category","df61_p8", True) # or simply, df61_p8['Category']='df61_p8' Out: code violation_description Category 89491 9-1-503 Defective or obstructed duct system one- building df61_p8 102045 9-1-503 Defective or obstructed duct system one- building df61_p8 103369 9-1-503 Defective or obstructed duct system one- building df61_p8 130440 9-1-502 Failure to maintain at least one (1) elevator df61_p8 132446 9-1-503 Defective or obstructed duct system one- building df61_p8  Ultimately, I want to append/concat these 8 dataframes into one dataframe. I wonder if there is more efficient way to do it, rather than using .insert one by one on each dataframe. Something like loops or lambdas.. As a beginner, I am not sure how to apply them in my case? thank you. append_alldfs = [] x=[df61_p1,df61_p2,df61_p3,df61_p4,df61_p5,df61_p6,df61_p7,df61_p8] lambdafunc = lambda x: x.insert(3,"Category","x",True) 

Answer: Keep it simple and explicit. for col_val, df in [ ('df61_p1', df61_p1), ('df61_p2', df61_p2), ('df61_p3', df61_p3), ('df61_p4', df61_p4), ('df61_p5', df61_p5), ('df61_p6', df61_p6), ('df61_p7', df61_p7), ('df61_p8', df61_p8), ]: df['Category'] = col_val  While there are certainly more 'meta-programming-ey' ways of accomplishing the same task, these are usually quite convoluted and more complicated to understand and refactor. Given the structure of this code, however, I imagine that there are ways you could get rid of this problem before you even get to this point. For example, at what point did those dataframes get split up? Perhaps by never using separate DataFrames in the first place [keep the original dataframe together/concat at beginning] (and using apply, groupby, pivot and melt operations as needed), you can avoid this problem altogether.
Question: I only want to show Chip, but I get both Chip AND Dale. It doesn't seem to matter which 32 bit character I put in, tkinter seems to duplicate them - it's not just chipmunks. I'm thinking that I may have to render them to png and then place them as images, but that seems a bit ... heavy-handed. Any other solutions? Is tkinter planning on fixing this? import tkinter as tk # Python 3.8.3 class Application(tk.Frame): def __init__(self, master=None): self.canvas = None self.quit_button = None tk.Frame.__init__(self, master) self.grid() self.create_widgets() def create_widgets(self): self.canvas = tk.Canvas(self, width=500, height=420, bg='yellow') self.canvas.create_text(250, 200, font="* 180", text='\U0001F43F') self.canvas.grid() self.quit_button = tk.Button(self, text='Quit', command=self.quit) self.quit_button.grid() app = Application() app.master.title('Emoji') app.mainloop()    Apparently this works fine on Windows - so maybe it’s a MacOS issue. I've run it on two separate Mac - both of them on the latest OS Catalina 10.15.5 - and both show the problem The bug shows with the standard Python installer from python.org - Python 3.8.3 with Tcl/Tk 8.6.8 Supposedly it might be fixed with Tcl/Tk 8.6.10 - but I don't really see how I can upgrade Tcl/Tk using the normal installer. This is also reported as a bug cf. https://bugs.python.org/issue41212  One of the python contributors believes that TCL/Tk can-not/will-not support variable width encoding (it always internally converts fixed width encoding) which indicates to me that Tcl/Tk is not suitable for general UTF-8 development.

Answer: The fundamental problem is that Tcl and Tk are not very happy with non-BMP (Unicode Basic Multilingual Plane) characters. Prior to 8.6.10, what happens is anyone's guess; the implementation simply assumed such characters didn't exist and was known to be buggy when they actually turned up (there's several tickets on various aspects of this). 8.7 will have stronger fixes in place (see TIP #389 for the details) — the basic aim is that if you feed non-BMP characters in, they can be got out at the other side so they can be written to a UTF-8 file or displayed by Tk if the font engine deigns to support them — but some operations will still be wrong as the string implementation will still be using surrogates. 9.0 will fix things properly (by changing the fundamental character storage unit to be large enough to accommodate any Unicode codepoint) but that's a disruptive change. With released versions, if you can get the surrogates over the wall from Python to Tcl, they'll probably end up in the GUI engine which might do the right thing. In some cases (not including any build I've currently got, FWIW, but I've got strange builds so don't read very much into that). With 8.7, sending over UTF-8 will be able to work; that's part of the functionality profile that will be guaranteed. (The encoding functions exist in older versions, but with 8.6 releases they will do the wrong thing with non-BMP UTF-8 and break weirdly with older versions than that.)
Question: Is there a way to run the command streamlit run APP_NAME.py from within a python script, that might look something like: import streamlit streamlit.run("APP_NAME.py")   As the project I'm working on needs to be cross-platform (and packaged), I can't safely rely on a call to os.system(...) or subprocess.

Answer: Hopefully this works for others: I looked into the actual streamlit file in my python/conda bin, and it had these lines: import re import sys from streamlit.cli import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(main())  From here, you can see that running streamlit run APP_NAME.py on the command line is the same (in python) as: import sys from streamlit import cli as stcli if __name__ == '__main__': sys.argv = ["streamlit", "run", "APP_NAME.py"] sys.exit(stcli.main())  So I put that in another script, and then run that script to run the original app from python, and it seemed to work. I'm not sure how cross-platform this answer is though, as it still relies somewhat on command line args.
Question: Due to AWS deprecating Signature Version 3 in Oct 2020 for SES, I want to handle this issue with AWS boto (Python). But I didn't see any doc related to boto supporting signature version 4 for SES. Is anyone having similar issue and have solutions?

Answer: My recommendation is that you migrate from boto, which is essentially deprecated, to boto3 because boto3 supports signature v4 by default (with the exception of S3 pre-signed URLs which has to be explicitly configured).
Question: for each subplot I have 3 seperate line:2017 ,2018 and 2019 with 3 times "go.Scatter", each subplot represents one country (25 countries) with always these 3 years. I can use the subplot sample code but then all the 75 legends (25 X 3) will be all together with different colors and it's messy. I don't need different colors amont different subplot, I can just have 3 different colors and 3 legends for the 3 years on all subplots, would be ideal if I click on for example 2017 that all the 2017 curve/line dissappear across the 25 subplots. Anyone can share a sample code? it can be 2 instead of 25 for illustration purpose. I fail to find this sample code on Plotly website. Edit: this is a sample code: from plotly.subplots import make_subplots import plotly.graph_objects as go from plotly import offline fig = make_subplots(rows=3, cols=1) fig.add_trace(go.Scatter( x=[3, 4, 5], y=[1000, 1100, 1200],name="2017", ), row=1, col=1) fig.add_trace(go.Scatter( x=[2, 3, 4], y=[1200, 1100, 1000],name="2018", ), row=1, col=1) fig.append_trace(go.Scatter( x=[2, 3, 4], y=[100, 110, 120],name="2017", ), row=2, col=1) fig.append_trace(go.Scatter( x=[2, 3, 4], y=[120, 110, 100],name="2018", ), row=2, col=1) fig.append_trace(go.Scatter( x=[0, 1, 2], y=[10, 11, 12],name="2017", ), row=3, col=1) fig.append_trace(go.Scatter( x=[0, 1, 2], y=[12, 11, 10],name="2018", ), row=3, col=1) fig.update_layout(height=600, width=600, title_text="Stacked Subplots") offline.plot(fig,filename="subplots.html")  I wish to have only 2 legends: 2017 and 2018, instead of 6 legends, easier if all the 2017 has same color along the 3 subplots

Answer: A correct combination of legendgroup and showlegend should do the trick. With the setup below, all 2017 traces are assigned to the same legendgroup="2017". And all 2017 traces except the first have showlegend=False. And of course the same goes for the 2018 traces. Give it a try! Plot  Complete code from plotly.subplots import make_subplots import plotly.graph_objects as go from plotly import offline fig = make_subplots(rows=3, cols=1) fig.add_trace(go.Scatter(x=[3, 4, 5], y=[1000, 1100, 1200], name="2017", legendgroup="2017", line=dict(color='blue')), row=1, col=1) fig.add_trace(go.Scatter(x=[2, 3, 4], y=[1200, 1100, 1000], name="2018",legendgroup="2018", line=dict(color='red')), row=1, col=1) fig.add_trace(go.Scatter(x=[2, 3, 4], y=[100, 110, 120], name="2017", legendgroup="2017", line=dict(color='blue'), showlegend=False), row=2, col=1) fig.append_trace(go.Scatter(x=[2, 3, 4], y=[120, 110, 100], name="2018", legendgroup="2018", line=dict(color='red'), showlegend=False), row=2, col=1) fig.append_trace(go.Scatter(x=[0, 1, 2], y=[10, 11, 12], name="2017", legendgroup="2017", line=dict(color='blue'), showlegend=False), row=3, col=1) fig.append_trace(go.Scatter(x=[0, 1, 2], y=[12, 11, 10], name="2018", legendgroup="2018", line=dict(color='red'), showlegend=False), row=3, col=1) fig.update_layout(height=600, width=600, title_text="Stacked Subplots") #offline.plot(fig,filename="subplots.html") fig.show()
Question: I am trying to read the following file line by line and check if a value exists in the file. What I am trying currently is not working. What am I doing wrong? If the value exists I do nothing. If it does not then I write it to the file. file.txt: 123 345 234 556 654 654  Code: file = open("file.txt", "a+") lines = file.readlines() value = '345' if value in lines: print('val ready exists in file') else: # write to file file.write(value) 

Answer: There are two problems here:  .readlines() returns lines with \n not trimmed, so your check will not work properly. a+ mode opens a file with position set to the end of the file. So your readlines() currently returns an empty list!  Here is a direct fixed version of your code, also adding context manager to auto-close the file value = '345' with open("file.txt", "a+") as file: file.seek(0) # set position to start of file lines = file.read().splitlines() # now we won't have those newlines if value in lines: print('val ready exists in file') else: # write to file file.write(value + "\n") # in append mode writes will always go to the end, so no need to seek() here  However, I agree with @RoadRunner that better is to just use r+ mode; then you don't need the seek(0). But the cleanest is just to split out your read and write phases completely, so you don't run into file position problems.