The Brazilian government has appointed an army general to oversee security in the state of Rio de Janeiro, in response to growing gang violence.
President Michel Temer compared the violence to "a cancer" and said organised criminals had all but seized control of the state.
Rio's governor issued an appeal for help after the annual carnival celebrations were marred by violence.
The army will oversee police and other security services.
Overseeing the operation will be Gen Walter Souza Braga Netto, head of the Eastern Military Command. He was widely praised for his part in co-ordinating security for the 2016 Rio Olympic Games.
What led to the situation?
Signing the decree, Mr Termer said he was taking "extreme measures" because circumstances demanded it.
"The government will give tough and firm answers, taking all necessary measures to eradicate organised crime," he said.
There were chaotic scenes during the famous Rio carnival, with gun fights and looting. Three police officers died in violent clashes.
National TV news bulletins also broadcast footage of gangs surrounding and robbing tourists.
With the security situation apparently spiralling out of control, state governor Luiz Fernando Pezao made a plea to the national government saying military intervention was the only way to tackle the heavily armed gangs.
He apologised to those revellers affected, saying: "We were not ready. There were mistakes in the first days and we reinforced the patrols."
Why has the violence worsened?
Rio's police budget has been slashed in recent years because of a financial crisis. There have been criticisms that police do not even have the money to pay for the petrol in their patrol cars.
Finances in Rio state have been badly hit by a national recession and a slump in oil prices, as well as high levels of corruption.
The financial problems have only emboldened criminal gangs.
Figures from the Rio state government show an 8% increase in killings last year over 2016 and a 26% jump since 2015.
'There's no escaping reality'
Julia Carneiro, BBC News, Rio de Janeiro
Stunned, numb, in shock or in denial. Living in Rio brings such mixed feelings today. Life continues as usual in the city and residents go about trying to lead their lives normally.
There's "nothing happening" – until something does. And more and more violence has been getting in the way, with stray bullets killing children in favelas, shoot-outs closing down major highways and mass robberies ruining the fun for several residents and tourists in the middle of the Carnival celebrations.
The saying goes in Brazil that the year starts once Carnival ends, and the intervention announced by the federal government reinforces the feeling that there's no escaping reality.
If Rio already felt misgoverned, the fact that the state governor willingly gave up his power for the president to call the shots in the security arena adds to the feeling of helplessness in the hands of unpopular politicians.
For now, there will be hope that at least something is being done. But Brazilians are weary of big political gestures – especially in the months leading up to a presidential election.
What happens now?
The army regularly patrols some of the most dangerous areas of Rio de Janeiro where drugs gangs hold sway, but now the military presence could be felt all over the city's metropolitan area of 12 million people and the wider state.
Correspondents say it will be the first time the army has had such a high profile since Brazil's return to democracy in 1985, which followed 21 years of military rule.
The move still needs to be approved by Brazil's National Congress.
"Organised crime has almost taken over the state of Rio de Janeiro, it is a cancer that spreads throughout the country and threatens the tranquillity of our people, so we have now enacted the federal intervention of the public security area of Rio de Janeiro," Mr Temer said.
Brazilian media, citing government sources, said the current head of public security, Roberto Sá, would be removed from office.
It is understood Gen Netto will be in charge of Rio state security at least until the end of the year.