I'm doing project at University right now where I have to develop a word learning/translating game where multiple clients compete to translate a words the fastest, handled by a central server.
The GUI for this uses Java Swing JFrames, and to get a good looking GUI with nicely placed texts and buttons and such, you need a lot of specific numbers for distances and widths and such.
Now my Professor is VERY strict about checkstyle-violations, and she provides her own checkstyle-files, which include checks on magic numbers...
Now a pretty simple code fragments with 2 methods (of 20) such as:
private void addLanguageText() {
languageText = new JLabel();
languageText.setText("Sprache auswählen:");
languageText.setBounds(20, 70, 150, 30);
frame.add(languageText);
}
private void addWelcomeText() {
welcomeText = new JLabel("Willkommen zum Lernspiel!", SwingConstants.CENTER);
welcomeText.setFont(welcomeText.getFont().deriveFont(20.0f));
welcomeText.setBounds(0, 10, 500, 30);
frame.add(welcomeText);
}
Already has 8 different magic numbers, so my checkstyle warnings have about 100 entrys for magic Numbers in total.
Now it seems pretty impossible to just add 100 number constants, escpacially since they would have to have names such as WELCOME_TEXT_LEFT_BOUNDS.
What would be an efficient solution for dealing with this?
There's two solutions to this problem: either pull every value out to a constant (which you stated would be unreasonable), or base those values on other, more universal, constants.
For example, in order to have a welcome text in the middle of the screen, halfway down, you could write something like:
welcomeText.setBounds(0, SCREEN_HEIGHT / 2, WELCOME_TEXT_WIDTH, WELCOME_TEXT_HEIGHT);
This solution, however, is effectively similar to using a LayoutManager, albeit with a bit more customizability. There really isn't any, reasonable, way to remove all the magic numbers, but you can hide them behind math or external libraries.
Edit for clarity:
Pulling out all of the magic numbers to their own constant isn't a good solution because doing so takes a long time and heavily limits the usability of your code in non-standard situations, hence why I recommend using a LayoutManager (or a self-made variant if you want to put in extra work). To emphasize, though, a LayoutManager does not remove magic numbers, they simply hide them from view. Visual interfaces usually don't use exact fractions of the screen, for aesthetic appeal.
Use layout managers. This way, your interface will not only be (almost) free of magic numbers -- it will also work as expected when windows are resized, or text suddenly takes more or less space than it used to (this happens often when you translate an app from one language to another).
In particular,
JFrames almost always benefit from a BorderLayout
GridBagLayout can take care of anything (but it is a hassle to learn at first). Nested BoxLayouts are easier to learn and use.
Simple popup dialogues can be built very easily using JOptionPanes, which include their own layout for accept/cancel/... buttons.
Remaining magic numbers should be things like "number of columns in this dialogue" or "uniform spacing used to separate form rows", and can be given meaningful constant names.
With layouts, you never need to call setBounds directly. Several widespread IDEs have built-in graphical support for playing with layouts, so do not need to dive as deep in the documentation as it may seem. I recomment NetBean's form editor.
Related
On these screenshots, you can see the difference between indent size.
The first one it's the default Code Style provided by Idea:
And the second one it's a Google Java Code Style (I renamed it a little but it's the same default file):
The second screen clearly shows that the indents a smaller in a half.
What's the point in reducing them? And the second question is, how can I undo this modification by modifying the file?
To allow long lines become shorter to make more free room on small screens.
The original 4 spaces indentation was intentionally introduced with the design of java (whereas the indentation of C/C++ was normally less), for a better overview and in order to punish too much nesting of blocks, using more sub-methods. And indeed this indentation does not hurt.
Google's later Java code style is an actionistic reform, which such a huge player like Google can push.
For a regular indention of code of 4 indents it would save only 8 characters.
This has minor effect when also considering the length of field/variable/method names.
So I would call the Google style hubris, a mistake for a language which has strong conventions. Other languages have more variations, especially on placement of parentheses and on spacing.
But one should adapt to the convention of the one of both, that is used in the firm. Google's style did not generally replace the old style, even though Android is a huge part on Google's home turf.
I've been playing a bit with some image processing techniques to do HDR pictures and similar. I find it very hard to align pictures taken in bursts... I tried some naïve motion search algorithms, simply based on comparing small samples of pixels (like 16x16) between different pictures that pretty much work like:
- select one 16x6 block in the first picture, one with high contrast, then blur it, to reduce noise
- compare in a neighbouring radius (also blurred for noise)... (usually using averaged squared difference)
- select the most similar one.
I tried a few things to improve this, for example using these search alghorithms (https://en.wikipedia.org/wiki/Block-matching_algorithm) to speed it up. The results however are not good and when they are they are not robust. Also they keep being computationally very intensive (which precludes usage on a mobile device for example).
I looked into popular research based algorithms like https://en.wikipedia.org/wiki/Lucas%E2%80%93Kanade_method, but it does not seem very suitable to big movements. If we see burst images taken with todays phones, that have sensors > 12Mpix, it's easy that small movements result in a difference of 50-100 pixels. The Lucas Kanade method seems more suitable for small amounts of motion...
It's a bit frustrating as there seem to be hundreds of apps that do HDR, and they seems to be able to match pictures so easily and reliably in a snap... I've tried to look into OpenCV, but all it offers seems to be the above Lucas Kanade method. Also I've seen projects like https://github.com/almalence/OpenCamera, which do this in pure java easily. Although the code is not easy (one class has 5k lines doing it all). Does anyone have any pointers to reliable resources.
Take a look at HDR+ paper by google. It uses a hierarchical algorithm for alignment that is very fast but not robust enough. Afterward it uses a merging algorithm that is robust to alignment failures.
But it may be a little tricky to use it for normal HDR, since it says:
We capture frames of constant exposure, which makes alignment more robust.
Here is another work that needs sub-pixel accurate alignment. It uses a refined version of the alignment introduced in the HDR+ paper.
HDR+ code
Relatively simple question. I need to translate/localize a legacy Java application.
Our company, with newer applications uses .properties files in Java for localizing their strings, and this concept is very similar to .resx files in C# (which we also have products using that).
The problem is this is a legacy product that was around before we started thinking about localization. It is full of hard coded strings and also various forms of hard-coded string concatenation/formatting.
As far as I am aware I have a very daunting task of pulling all our strings and formatting into .properties files in the product and then referencing those in the code.
Personally I have no huge issue doing this work, but I want to make sure I am not missing something.
So I have a couple general questions.
Is there a faster way of converting my product to use the
.properties files? Off the top of my head I could write a script
that would automate maybe 30-40% of the work...
Are there any "gotchas" I should be worried about specific to converting a legacy
product (I am not looking for general localization "gotchas" which I
can google for, but anything specific to this scenario)?
Finally, are there any completely different strategies I am overlooking for
localization? This is just how we translate our existing products,
but because this is a legacy product (and on the agenda to be
re-written) this is essentially throw-away code and I could do pretty much whatever I want. Including just
finding the cheapest dirtiest fastest way possible, although I am
obviously leaning toward doing the job properly.
Any thoughts, people?
As a guideline I would say try to keep answers focused on the questions being asked, but any informational contributions or questions are always welcome in comments.
No, there is no faster way. You have to go through the code line by line.
There are plenty of gotchas, since internationalization is about more than just string constants.
You may already know that number formats and date formats need to be localized, but you'll need to be on the lookout for numbers and dates being embedded into strings via concatenation or StringBuilder.append calls. You'll also need to be on the lookout for implicit toString() calls, such as when a Number or Date is supplied as a Swing model value (for example, returning a Number from the TableModel.getValueAt method), or when a JSP or JSF EL expression refers to such a value directly instead of formatting it.
Similarly, keep an eye out for enum constants directly displayed to the user, implicitly invoking their toString() method.
Creating sentences through string concatenation is a problem not only because of the formatting of numbers, dates, and enums, but also because other languages may have different ordering of sentence structure. Such string concatenation should be replaced with localized MessageFormats.
Keystrokes need to be localized, including all mnemonics (and accelerators if it's a desktop app).
Layouts are an issue. Places where the application assumes left-to-right orientation are something you'll want to address; even if you're only planning to localize for other left-to-right languages, you probably know that putting off good i18n practices is asking for trouble later down the line.
If your app is a Swing application, you'll want to convert LEFT/WEST and RIGHT/EAST layout constraints to LINE_START and LINE_END. If your app is a web application, you'll need to factor out margin-left, margin-right, padding-left, padding-right, border-left, and border-right (and probably many others I'm forgetting) into lang-specific CSS blocks.
Swing apps also need to call applyComponentOrientation after building each window, usually right before calling pack().
Some programmers like to store parts of a UI in a database. I'm not talking about user content (which you shouldn't localize); I'm talking about label text, window titles, layout constraints, and so on. I have a hearty dislike for that practice, personally, but people do it. If your app is doing that, I guess either the database table needs a locale column, or the practice of storing the UI in the database needs to be removed entirely.
To answer your final question, if there are any better strategies than stepping through the code, I've never heard of them. You could just search for double-quote characters in the code, of course. I suppose the choice depends on how professional and polished your superiors want the application to look.
One thing I've learned is that throw-away code often isn't. Don't be surprised if that rewrite ends up trying to salvage large swaths of code from the legacy version.
I want to implement object detection in license plate (the city name) . I have an image:
and I want to detect if the image contains the word "بابل":
I have tried using a template matching method using OpenCV and also using MATLAB but the result is poor when tested with other images.
I have also read this page, but I was not able to get a good understanding of what to do from that.
Can anyone help me or give me a step by step way to solve that?
I have a project to recognize the license plate and we can recognize and detect the numbers but I need to detect and recognize the words (it is the same words with more cars )
Your question is very broad, but I will do my best to explain optical character recognition (OCR) in a programmatic context and give you a general project workflow followed by successful OCR algorithms.
The problem you face is easier than most, because instead of having to recognize/differentiate between different characters, you only have to recognize a single image (assuming this is the only city you want to recognize). You are, however, subject to many of the limitations of any image recognition algorithm (quality, lighting, image variation).
Things you need to do:
1) Image isolation
You'll have to isolate your image from a noisy background:
I think that the best isolation technique would be to first isolate the license plate, and then isolate the specific characters you're looking for. Important things to keep in mind during this step:
Does the license plate always appear in the same place on the car?
Are cars always in the same position when the image is taken?
Is the word you are looking for always in the same spot on the license plate?
The difficulty/implementation of the task depends greatly on the answers to these three questions.
2) Image capture/preprocessing
This is a very important step for your particular implementation. Although possible, it is highly unlikely that your image will look like this:
as your camera would have to be directly in front of the license plate. More likely, your image may look like one of these:
depending on the perspective where the image is taken from. Ideally, all of your images will be taken from the same vantage point and you'll simply be able to apply a single transform so that they all look similar (or not apply one at all). If you have photos taken from different vantage points, you need to manipulate them or else you will be comparing two different images. Also, especially if you are taking images from only one vantage point and decide not to do a transform, make sure that the text your algorithm is looking for is transformed to be from the same point-of-view. If you don't, you'll have an not-so-great success rate that's difficult to debug/figure out.
3) Image optimization
You'll probably want to (a) convert your images to black-and-white and (b) reduce the noise of your images. These two processes are called binarization and despeckling, respectively. There are many implementations of these algorithms available in many different languages, most accessible by a Google search. You can batch process your images using any language /free tool if you want, or find an implementation that works with whatever language you decide to work in.
4) Pattern recognition
If you only want to search for the name of this one city (only one word ever), you'll most likely want to implement a matrix matching strategy. Many people also refer to matrix matching as pattern recognition so you may have heard it in this context before. Here is an excellent paper detailing an algorithmic implementation that should help you immensely should you choose to use matrix matching. The other algorithm available is feature extraction, which attempts to identify words based on patterns within letters (i.e. loops, curves, lines). You might use this if the font style of the word on the license plate ever changes, but if the same font will always be used, I think matrix matching will have the best results.
5) Algorithm training
Depending on the approach you take (if you use a learning algorithm), you may need to train your algorithm with data that is tagged. What this means is that you have a series of images that you've identified as True (contains city name) or False (does not). Here's a psuedocode example of how this works:
train = [(img1, True), (img2, True), (img3, False), (img4, False)]
img_recognizer = algorithm(train)
Then, you apply your trained algorithm to identify untagged images.
test_untagged = [img5, img6, img7]
for image in test_untagged:
img_recognizer(image)
Your training sets should be much larger than four data points; in general, the bigger the better. Just make sure, as I said before, that all the images are of an identical transformation.
Here is a very, very high-level code flow that may be helpful in implementing your algorithm:
img_in = capture_image()
cropped_img = isolate(img_in)
scaled_img = normalize_scale(cropped_img)
img_desp = despeckle(scaled_img)
img_final = binarize(img_desp)
#train
match() = train_match(training_set)
boolCity = match(img_final)
The processes above have been implemented many times and are thoroughly documented in many languages. Below are some implementations in the languages tagged in your question.
Pure Java
cvBlob in OpenCV (check out this tutorial and this blog post too)
tesseract-ocr in C++
Matlab OCR
Good luck!
If you ask "I want to detect if the image contains the word "بابل" - this is classic problem which is solved using http://code.opencv.org/projects/opencv/wiki/FaceDetection like classifier.
But I assume you still want more. Years ago I tried to solve simiar problems and I provide example image to show how good/bad it was:
To detected licence plate I used very basic rectangle detection which is included in every OpenCV samples folder. And then used perspective transform to fix layout and size. It was important to implement multiple checks to see if rectangle looks good enough to be licence plate. For example if rectangle is 500px tall and 2px wide, then probably this is not what I want and was rejected.
Use https://code.google.com/p/cvblob/ to extract arabic text and other components on detected plate. I just had similar need yesterday on other project. I had to extract Japanese kanji symbols from page:
CvBlob does a lot of work for you.
Next step use technique explained http://blog.damiles.com/2008/11/basic-ocr-in-opencv/ to match city name. Just teach algorithm with example images of different city names and soon it will tell 99% of them just out of box. I have used similar approaches on different projects and quite sure they work
For a specific GUI of my java web application, some members of my team have designed different web GUIs. Each of the designed GUIs has its own pros and cons. Is there a metric to evaluate a web GUI and score each designed one a “goodness mark”? I want to pick up that GUI that has the highest score. Or any tools is available for this evaluation?
Not really, because "goodness" means different things to different people. On a web site you might measure that as how many people make it to the checkout though different interface variations, or how quickly they find a page they're looking for or how long they stay on the site etc.
You could measure some things against known standards, accessibility for example and there are some very general design principles (a grid layout is generally better than random jumble for example, consistency in placement and ordering of buttons is another).
If your application has a sufficiently high number of users to make a sensible sample size then a typical approach is A/B testing where you serve different versions of the interface to different people and see what happens. However, you still need to decide on a goal to measure against (number of sales, time spent on site etc) to compare the two versions.
If you have a small user base or this is an internal application then direct user feedback might be the better option - i.e. just go and ask them which they find easier/better to use.
You can try the "usefulness" vs "easy to use" metric.
Get some of your users sit down to rate UI with -10 to 10 on both variable, and plot them on the graph with different color, obviously more dots on top left is the better one.
I use this approach a lot as it can be done in like 10 minutes, and you can switch the "variable" depend on the project scope.
just my 2 cents
Test it on real customers. The one they feel most content with is the one they are most likely to continue paying you for. That's the best GUI.
This is really a personal call, depending on the intended purpose of the GUI, and its future users. If it's intended for sight-impaired people, I would use large and clear fonts and buttons, and possibly sound. It really depends on what you need.
You should make your own metric, and sort (or even give weights to) the options according to the desired preferences.
For accessibility, there are automatic tools, e.g. HTML Validator for firefox.