yolo9000 writing bounding box coordinates as .txt - output

I have been trying to write bounding box coordinates along with detected object name and the confidence value as a text file and output it as .txt file but in vain. I want something like following written as a .txt file:
[{"label":"person", "confidence": 0.56, "left": 184, "bottom": 101, "right": 274, "top": 382}]
I am relatively new in c programming language and yolo9000 is written in darknet in c. I am following the below implementation:
https://github.com/philipperemy/yolo-9000

Related

How to group blocks that are part of a bigger sentences in Google Cloud Vision API?

I am using Google Cloud Vision API on Python to detect text values in hoarding boards that are usually found above a shop/store. So far I have been able to detect individual words and their bounding polygons' coordinates. Is there a way to group the detected words based on their relative positions and sizes?
For example, the name of the store is usually written in same size and the words are aligned. Does the API provide some functions that group those words which probably are parts of a bigger sentence (the store name, or the address, etc.)?
If the API does not provide such functions, what would be a good approach to group them? Following is an example of an image what I have done so far:
Vision API output excerpt:
description: "SHOP"
bounding_poly {
vertices {
x: 4713
y: 737
}
vertices {
x: 5538
y: 737
}
vertices {
x: 5538
y: 1086
}
vertices {
x: 4713
y: 1086
}
}
, description: "OVOns"
bounding_poly {
vertices {
x: 6662
y: 1385
}
vertices {
x: 6745
y: 1385
}
vertices {
x: 6745
y: 1402
}
vertices {
x: 6662
y: 1402
}
}
I suggest you to take a look on the TextAnnotation response format that is applied when using the DOCUMENT_TEXT_DETECTION for OCR recognition request. This responses contains detailed information about the image metadata and text content values that can be used to group the text by block, paragraph, word, etc, as described in the public documentation:
TextAnnotation contains a structured representation of OCR extracted text. The hierarchy of an OCR extracted text structure is like this: TextAnnotation -> Page -> Block -> Paragraph -> Word -> Symbol
Additionally, you can follow this useful example where is shown how you can organize the text extracted from a receipt image by processing the fullTextAnnotation response content.

plotting maps using OSM or other shapefiles and matplotloib for standardized report

We are developing a standardized report for our activities. The last graph I need is to display the geographic area of the activities (there are close to 100 locations).
The output for these reports is PDF letter or A4 size
The report is a mplotlib figure, where:
fig = plt.figure(figsize=(8.5, 11))
rect0 = 0, .7,, 0.18, 0.3
rect1 = .3, .7, .18, .3
rect2 = .8, .29, .2, .7
rect3 = 0, 0, .8, .4
ax1 = fig.add_axes(rect0)
ax2 = fig.add_axes(rect1)
ax3 = fig.add_axes(rect2)
ax4 = fig.add_axes(rect3)
The contents and layout for axes 1-3 are settled and work great. However ax4 is where the map contents would be displayed (ideally).
I was hoping to do something like this:
map1 = Basemap(llcrnrlon=6.819087, llcrnrlat=46.368452, urcrnrlon=6.963978,
urcrnrlat=46.482906, resolution = 'h', projection='tmerc',
lon_0=6.88, lat_0=46.42, ax=4)
map1.readshapefile('a valid shape file that works') #<----- this is the sticking point
map1.draw(insert locator coordinates)
plt.savefig(report to be inserted to document)
plt.show()
However I have not been successful in obtaining a shape file that works from open street maps or GIS.
Nor have I identified the correct process to transform the data from openstreetmaps.
Nor have I identified the process to extract that information from the OSM/xml document or the transformed GeoJSON document.
Ideally I would like to grab the bounding box information from openstreetmaps and generate the map directly.
What is the process to get a shapefile that works with the .readshapefile() call?
Or alternatively how do I get the defined map into a Matplotlib axes ?
It might be easiest to use the cartopy.io.img_tiles module, which will automatically pull the OSM tiles for use with cartopy. Using the pre-rendered tiles would negate the trouble of handling and styling individual shapefiles/XML.
See the cartopy docs on using these tiles within cartopy.

TurfJs union - How to ignore point which are inside but with little bit difference in union?

I have used 2 geojson object for polygon. It's too large that I can't post it here. Now I am using TurfJs to make the union of this polygon geojson and plotting it on the map. But it's not working properly.
I think little bit points in the middle of it is a little bit different. So is there any way to ignore this points in the middle in turfjs union?
See images bellow for better understanding.
Polygon 1 :
Polygon 2 :
Now merged polygon for bellow code:
polygons = {
"type": "FeatureCollection",
"features": [poly1, poly2]
};
Now main UNION result:
union = turf.union(poly1,poly2);
So in this, i want to ignore points that are in middle of boundary I know that, there may be points that are not accurate on intersection boundary of both polygon but can I ignore points that are nearer or having little bit of differencr to ignore middle points?
Or is there is any alternative to do union of polygon that ignore few nearer distraction of point and remove middle points?
You can try running the resulting polygon through turf.buffer(result, 0, 'kilometers') (turf-buffer docs). If your result is invalid geojson then using a buffer of 0 should cleanse the geometry (remove the points/lines in the middle).
It is hard to say what will work for sure without seeing the actual GeoJSON of the result. Is there any way you can upload it to pastebin or something?
Update - Turf buffer did not work in this case. The only solution that I could get to work was doing this on the result of turf.union(p1, p2).
result.geometry.coordinates = [result.geometry.coordinates[0]]
You want to be careful with this solution as it removes everthing from the polygon other than the external ring.
To understand why/how this works, you will want to make sure you understand how the coordinates for geojson polygons work. From the geojson.org geojson polygon specification
For type "Polygon", the "coordinates" member must be an array of LinearRing coordinate arrays. For Polygons with multiple rings, the first must be the external ring and any others must be internal rings or holes.
The external ring is essentially the outline of your polygon. Any internal rings are usually represented as holes. In your case, the internal rings were actually lines.
When looking at the coordinates for a geojson polygon, you will notice that all coordinates are contained within an outer array. Here is an example of a geojson polygon with only a single (external) ring.
{"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": **[ [ [1, 1], [1, 2], [1, 3], [1, 1] ] ]**
Notice that the first coordinate and last coordinate of a ringe must always be the same. That ensure that we get a closed shape (ie: polygon).
Now here is an example with an external ring, and an internal ring
{"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": **[ [ [1, 1], [1, 2], [1, 3], [1, 1] ], [ [1, 2], [1, 3], [1, 1] ] ]**
Now if we apply the suggested solution to the above example, we would get the same coordinates as the first example because we are grabbing only the first set of coordinates from the polygon, which will always be the external ring. Any subsequent elements in the coordinates array will represent internal rings (which is what the lines were, even though they are technically not valid internal rings).
{"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": **[ [ [1, 1], [1, 2], [1, 3], [1, 1] ] ]**
As you can see, we are removing all internal rings from the polygon. That is the reason that you must be careful with how you use this. If you ever have valid internal rings, it will actually get rid of those.
I think that the reason this happens is because your polygons (p1 and p2) share a border.
Faced the same problem: After trying to buffer a small positive amount and the same negative amount, the lines disappears. But this made the polygon having more points than the original so I did this workaround:
inner = [YOUR FEATURE COLLECTION]
var areas = []
for (var i = 0; i < inner.geometry.coordinates.length; i++) {
let item = inner.geometry.coordinates[i]
if (item.length > 10) areas.push(item)
}
inner = turf.polygon(areas)
As you can see I am removing the "non complex" polygons (assuming that a polygon with less than 10 points is not a real area)
This happens because the coordinates of both polygons are not 100% the same, creating a small gap when merging them together.
When faced with this problem, I had to use turf's distance method to check every vertex of the polygons, and if there was a small difference between them, I'd make them the same.
The implementation method may vary on the map library you are using, but it should go something like this:
layers.forEach(layer => {
layers.forEach(innerLayer => {
if (layer === innerLayer) return;
// Here you would check if the vertexes are close to each other, using distance.
// If the vertexes are close, you would make them equal and update the layer.
})
})
Only after making the vertex of the polygons the same, you would merge them with the union method.
Since the implementation is pretty unique and depends on the project, I won't waste both our time with actual code, but I believe that with the insights above, you should be good to go.

Node generation from json data for force layout

I'm trying to do a bubble chart similar to Mike Bostock's:
However, instead of randomly generating the nodes, I want them to be generated for a json. Suppose I have a dataset variable that is a valid JSON how do I generate a nodes variable from this that can be used for the force layout.
Mike's implementation (for randomly-generated circles) is:
var nodes = d3.range(n).map(function() {
var i = Math.floor(Math.random() * m),
v = (i + 1) / m * -Math.log(Math.random());
return {
radius: radius(v),
color: color(i),
cx: x(i),
cy: height / 2
};
});
Let's suppose my dataset JSON is something like:
{"nodes":[{"name": "node_name", "size": 18}, ... ]}
And I want the radius of each circle to be the size of each object in the JSON array.
nodes is an array of objects which each have radius, color, cx and cy attributes. All you need to do is create such an array from your data. How exactly to do this will depend on your data. Note that you don't need to create all the attributes -- if all the nodes can have the same color for example, you don't need to add the corresponding attribute (but also need to change the code that actually sets the color later on).

Best line equation to use for computational geometry

I'm looking to write a little comp-geom library, in Ruby.
I'm about to write the code for lines, and was wondering which line equation I should use:
ax + by + c = 0
r + tv (where r and v are vectors)
Thanks.
If using the classical equations is not a requirement, I'd suggest an array of four co-ordinates: xStart, yStart, xEnd and yEnd.
In case you need to make the line position dynamic, you could use an array of two parameters: alpha and radius. The former represents radial rotation relative to the horizontal axis and the latter is the length of line.
Yet another option would be vectors in the form of (X;Y).
Samples in C:
int endpointsLine[4] = {0, 0, 30, 40};
double radialLine[2] = {5.35589, 50};
int vectorLine[2] = {30, 40};
The "endpoints" format is fully compatible with modern line-drawing algorithms, such as Xiaolin Wu's line algorithm and Bresenham's line algorithm but it represents specific screen co-ordinates which is not the case with "radial" and "vector" format.